You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何修改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:

  1. Added Folder Creation: The script now checks if C:\OutlookEmails\ exists, and creates it if it doesn't (prevents runtime errors).
  2. Replaced item.SaveAs: Instead of exporting the full email, we use Scripting.FileSystemObject to create a new TXT file and write only item.Body (the plain text email content) into it.
  3. Simplified Filename Format: Combined the two Format calls into one for cleaner code (same end result).
  4. 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:

  1. Open Outlook, press Alt + F11 to open the VBA Editor.
  2. Paste this code into your existing module (or create a new one).
  3. Go back to Outlook, select one or more emails in your inbox.
  4. Press Alt + F8, select Export_MailBodyAsTxt, and click "Run".

Your TXT files will now contain only the email body, no headers!

内容的提问来源于stack exchange,提问作者mickmickmick

火山引擎 最新活动