Skip to main content

如何在Outlook中導出聯絡人的信息以及照片?

Author: Xiaoyang Last Modified: 2025-05-12

當您從Outlook導出聯絡人到文件時,只能導出聯絡人的文字信息。但是,有時候您需要同時導出照片和聯絡人的文字信息,該如何在Outlook中處理這個任務呢?

使用VBA代碼導出帶有相關照片的聯絡人信息


使用VBA代碼導出帶有相關照片的聯絡人信息

以下VBA代碼可以幫助您將特定聯絡人文件夾中的所有聯絡人導出到單獨的文字文件中,並附帶照片。請按照以下步驟操作:

1. 選擇要導出帶有照片的聯絡人的聯絡人文件夾。

2. 接著,按住「ALT」+「F11」鍵以打開「Microsoft Visual Basic for Applications」窗口。

3. 然後,點擊「插入」>「模塊」,複製並將以下代碼粘貼到打開的空白模塊中,參見截圖:

VBA代碼:導出帶有照片的聯絡人信息

Sub BatchExportContactPhotosandInformation()
Dim xContactItems As Outlook.Items
Dim xItem As Object
Dim xContactItem As ContactItem
Dim xContactInfo As String
Dim xShell As Object
Dim xFSO As Scripting.FileSystemObject
Dim xTextFile As Scripting.TextStream
Dim xAttachments As Attachments
Dim xAttachment As Attachment
Dim xSavePath, xEmailAddress As String
Dim xFolder As Outlook.Folder
On Error Resume Next
Set xFSO = CreateObject("Scripting.FileSystemObject")
Set xShell = CreateObject("Shell.application").BrowseforFolder(0, "Select a Folder", 0, 16)
If xShell Is Nothing Then Exit Sub
xSavePath = xShell.Items.Item.Path & "\"
If Outlook.Application.ActiveExplorer.CurrentFolder.DefaultItemType <> olContactItem Then
    Set xFolder = Outlook.Application.Session.GetDefaultFolder(olFolderContacts)
Else
    Set xFolder = Outlook.Application.ActiveExplorer.CurrentFolder
End If
Set xContactItems = xFolder.Items
For i = xContactItems.Count To 1 Step -1
    Set xItem = xContactItems.Item(i)
    If xItem.Class = olContact Then
        Set xContactItem = xItem
        With xContactItem
            xEmailAddress = .Email1Address
            If Len(Trim(.Email2Address)) <> 0 Then
                xEmailAddress = xEmailAddress & ";" & .Email2Address
            End If
            If Len(Trim(.Email3Address)) <> 0 Then
                xEmailAddress = xEmailAddress & ";" & .Email3Address
            End If
            xContactInfo = "Name: " & .FullName & vbCrLf & "Email: " & _
                           xEmailAddress & vbCrLf & "Company: " & .CompanyName & _
                           vbCrLf & "Department: " & .Department & _
                           vbCrLf & "Job Title: " & .JobTitle & _
                           vbCrLf & "IM: " & .IMAddress & _
                           vbCrLf & "Business Phone: " & .BusinessTelephoneNumber & _
                           vbCrLf & "Home Phone: " & .HomeTelephoneNumber & _
                           vbCrLf & "BusinessFax Phone: " & .BusinessFaxNumber & _
                           vbCrLf & "Mobile Phone: " & .MobileTelephoneNumber & _
                           vbCrLf & "Business Address: " & .BusinessAddress
            Set xTextFile = xFSO.CreateTextFile(xSavePath & .FullName & ".txt", True)
            xTextFile.WriteLine xContactInfo
            If .Attachments.Count > 0 Then
                Set xAttachments = .Attachments
                For Each xAttachment In xAttachments
                    If InStr(LCase(xAttachment.FileName), "contactpicture.jpg") > 0 Then
                        xAttachment.SaveAsFile (xSavePath & .FullName & ".jpg")
                    End If
                Next
            End If
        End With
    End If
Next i
End Sub
doc export contacts with photos 1

4. 將代碼粘貼到模塊後,在「Microsoft Visual Basic for Applications」窗口中繼續點擊「工具」>「引用」,在彈出的「引用-Project1」對話框中,從「可用引用」列表框中勾選「Microsoft Scripting Runtime」選項,參見截圖:

doc export contacts with photos 2

5. 點擊「確定」關閉對話框,然後按下「F5」鍵運行此代碼,在彈出的「瀏覽文件夾」對話框中,指定要輸出導出聯絡人的文件夾,參見截圖:

doc export contacts with photos 3

6. 然後點擊「確定」,所有帶有照片的聯絡人信息都已分別導出到您的指定文件夾中,參見截圖:

doc export contacts with photos 4