Outlook VBA - 在約會中開啟電子郵件附件並提取寄件者的電子郵件地址

Outlook VBA - 在約會中開啟電子郵件附件並提取寄件者的電子郵件地址

我有 200 多個日曆(約會)條目,所有條目都包含原始電子郵件作為附件。我需要從約會的電子郵件附件中提取寄件者的電子郵件地址。

我知道如何從 MailItem 物件 ( MailItem.SenderEmailAddress) 中提取寄件者的電子郵件地址,但當電子郵件現在是約會中的附件時,我不知道如何存取這些屬性。該AppointmentItem物件具有 Attachments 屬性,但沒有任何其他資訊可用於說明如何存取 Attachments 物件中的任何屬性。嘗試 AppointmentItem.Attachments.item(1).SenderEmailAddress 並得到“不支援物件...”

答案1

您可以儲存 .msg 附件,作為郵件項目打開,然後讀取郵件項目屬性。

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

相關內容