【问题标题】:How to get the Body of an email in a selection of emails?如何在选择的电子邮件中获取电子邮件的正文?
【发布时间】:2022-08-24 19:55:00
【问题描述】:

获取电子邮件正文的命令不是从电子邮件中提取正文,而是在选择的电子邮件中循环处理。

如何设置 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

    标签: vba outlook


    【解决方案1】:

    无需在代码中设置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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 2013-04-30
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多