如何修改Outlook VBA脚本仅提取邮件正文至TXT文件?
How to Export Only Outlook Email Body to TXT (Without Headers)
Got it, let's get this sorted for you! The problem with your original code is that item.SaveAs exports the entire email package (including From/Sent/To/Subject headers), and item.Body.SaveAs doesn't work because Body is just a plain text string, not an object with a SaveAs method.
Here's the fixed VBA script that will only export the email body to your TXT file, while keeping your existing valid filename logic:
' General Declarations Option Explicit ' Public declarations Public Enum olSaveAsTypeEnum olSaveAsTxt = 0 olSaveAsRTF = 1 olSaveAsMsg = 3 End Enum Sub Export_MailBodyAsTxt(item As Outlook.MailItem) ' Error Handling On Error Resume Next ' Variable Declarations Dim strExportFolder As String: strExportFolder = "C:\OutlookEmails\" Dim strExportFileName As String Dim strExportPath As String Dim strReceivedTime As String Dim strSubject As String Dim objRegex As Object Dim fso As Object ' File System Object to handle file writing ' Create export folder if it doesn't exist (prevents "folder not found" errors) Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(strExportFolder) Then fso.CreateFolder strExportFolder End If ' Initiate regex search to clean invalid filename characters Set objRegex = CreateObject("VBScript.RegExp") With objRegex .Pattern = "(\s|\\|/|<|>|\||\?:)" .Global = True .IgnoreCase = True End With ' Process only if selected item is a mail item If TypeOf item Is Outlook.MailItem Then ' Format the file name (same logic as before, just cleaner) strReceivedTime = item.ReceivedTime strSubject = item.Subject strExportFileName = Format(strReceivedTime, "yyyymmdd-hhnnss") & "-" & strSubject strExportFileName = objRegex.Replace(strExportFileName, "_") ' Define full path for the TXT file strExportPath = strExportFolder & strExportFileName & ".txt" ' Write ONLY the email body to the TXT file Dim fileStream As Object Set fileStream = fso.CreateTextFile(strExportPath, True) ' True = overwrite existing file fileStream.Write item.Body fileStream.Close ' Optional: Uncomment to get a confirmation message ' MsgBox ("Email body saved to: " & strExportPath) Else MsgBox "Selected item is not an email!" End If ' Clean up memory Set item = Nothing Set objRegex = Nothing Set fso = Nothing Set fileStream = Nothing End Sub
Key Changes Explained:
- Added Folder Creation: The script now checks if
C:\OutlookEmails\exists, and creates it if it doesn't (prevents runtime errors). - Replaced
item.SaveAs: Instead of exporting the full email, we useScripting.FileSystemObjectto create a new TXT file and write onlyitem.Body(the plain text email content) into it. - Simplified Filename Format: Combined the two
Formatcalls into one for cleaner code (same end result). - Added Feedback for Non-Email Items: The else block now shows a message if you select something that's not an email.
How to Use:
- Open Outlook, press
Alt + F11to open the VBA Editor. - Paste this code into your existing module (or create a new one).
- Go back to Outlook, select one or more emails in your inbox.
- Press
Alt + F8, selectExport_MailBodyAsTxt, and click "Run".
Your TXT files will now contain only the email body, no headers!
内容的提问来源于stack exchange,提问作者mickmickmick




