跳至主要内容

如何在Outlook中导出联系人的信息以及照片?

Author: Xiaoyang Last Modified: 2025-05-07

当您从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