Outlook VBA-在约会中打开电子邮件附件并提取发件人的电子邮件地址

Outlook VBA-在约会中打开电子邮件附件并提取发件人的电子邮件地址

我有超过 200 个日历(约会)条目,所有条目都包含原始电子邮件消息作为附件。我需要从约会中的电子邮件附件中提取发件人的电子邮件地址。

我知道如何从 MailItem 对象 ( MailItem.SenderEmailAddress) 中提取发件人的电子邮件地址,但当电子邮件现在是约会中的附件时,我不知道如何访问这些属性。该AppointmentItem对象具有 Attachments 属性,但没有任何其他信息可用于了解如何访问 Attachments 对象中的任何属性。尝试了 AppointmentItem.Attachments.item(1).SenderEmailAddress 并得到“对象不受支持...”

答案1

您可以保存 .msg 附件,作为 mailitem 打开,然后读取 mailitem 属性。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant


Private Sub saveAndRetrievePropertyOfAttachedMail()

    Dim sPath As String
    
    ' the selected item may not be an AppointmentItem
    Dim oItem As Object
    
    sPath = ""
    
    Set oItem = ActiveExplorer.Selection.Item(1)
    
    If TypeName(oItem) = "AppointmentItem" Then
        Debug.Print oItem.subject
        
        ' sPath is initially blank
        ' There are risks with public variables.
        ' This is a slightly awkward way to avoid declaring sPath as a public variable
        saveAttachmentsToDriveFolder oItem, sPath
        
        returnPropertiesOfAttachmentInDriveFolder sPath
        
    End If
    
End Sub


Sub saveAttachmentsToDriveFolder(objItem, strPath)

    Dim fso As Object
    Dim fldTemp As Object
    
    Dim objAtt As Object
    Dim strFile As String
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' This is a default folder everyone should have.
    ' You may use another folder.
    ' Kill the files when you are done or delete manually
    Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
    Debug.Print fldTemp
    
    strPath = fldTemp.Path & "\"
    Debug.Print strPath
    
    For Each objAtt In objItem.Attachments
        strFile = strPath & objAtt.FileName
        Debug.Print strFile
        objAtt.SaveAsFile strFile
    Next
    
End Sub


Sub returnPropertiesOfAttachmentInDriveFolder(strAttachmentFolder)

Dim objFileSystem As Object
Dim objFolder As Object
Dim objFiles As Object
Dim objFile As Object
Dim objItem As Object

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFileSystem.GetFolder(strAttachmentFolder)
Set objFiles = objFolder.Files

For Each objFile In objFiles

    If objFileSystem.GetExtensionName(objFile) = "msg" Then
    
        'Open msg file
        Set objItem = Session.OpenSharedItem(objFile.Path)
        Debug.Print objItem.SenderEmailAddress

    End If
Next

End Sub

相关内容