我没有正确设置 MailItem。获取电子邮件正文的命令不会从活动电子邮件中提取正文

I am not setting the MailItem correctly. The command to get the Body of the email is not pulling the body from the active email

我是 VBA 的初学者。我没有正确设置 MailItem。获取电子邮件正文的命令不会从活动电子邮件中提取正文。不确定如何为“sText = itm.Body”命令正确设置 itm。

Dim itm         As Outlook.MailItem
Dim currentExplorer As Explorer
Dim Selection   As Selection
Dim strFileName As String, strExt As String
Dim objAtt      As Outlook.Attachment
Dim saveFolder  As String
Dim email_date  As Date
Dim date_ext    As String
Dim sText       As String
Dim Intext_date As String
Dim Mail_date   As Date
Dim email_date_temp As String

saveFolder = "C:\elan\Various\email_attachments\"

Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection

For Each itm In Selection
    For Each objAtt In itm.Attachments
        
        strFileName = objAtt.DisplayName
        
        ' get the last 5 characters or last 4 for .xls for the file extension
        strExt = Right(objAtt.DisplayName, 5)
        
        If Mid(strExt, 1, 1) <> "." Then
            strExt = Right(objAtt.DisplayName, 4)
            
        End If
        If strExt = ".xls" Or strExt = ".xlsx" Then
            ' clean the File Name
            ReplaceCharsForFileName strFileName, "-"
            
            ' Get Body of email
            
            Set itm = ActiveExplorer.Selection.item(1)
            sText = itm.Body
            Debug.Print sText

代码中不需要设置itm对象,如下:

Set itm = ActiveExplorer.Selection.item(1)

在您的代码中,itm 对象已分配给每个循环迭代:

For Each itm In Selection

因此,代码可能如下所示:

For Each itm In Selection
    For Each objAtt In itm.Attachments
        
        strFileName = objAtt.DisplayName
        
        ' get the last 5 characters or last 4 for .xls for the file extension
        strExt = Right(objAtt.DisplayName, 5)
        
        If Mid(strExt, 1, 1) <> "." Then
            strExt = Right(objAtt.DisplayName, 4)
            
        End If
        If strExt = ".xls" Or strExt = ".xlsx" Then
            ' clean the File Name
            ReplaceCharsForFileName strFileName, "-"
            
            ' Get Body of email           
            
            sText = itm.Body
            Debug.Print sText

请记住,您在内部循环中获取邮件正文,这意味着如果您有多个附加文件,您将为每个文件获取相同的邮件正文。