如何使用VBA打印每个Outlook联系人字段的名称和值

时间:2016-06-07 17:32:01

标签: vba outlook outlook-vba

对于每个Outlook联系人,我需要获取每个字段的名称和值 例如

    FirstName:  John
    LastNmae:  Doe
... etc.

如何在不单独引用每个字段的情况下进行此操作?

使用下面的代码我可以打印每个属性的名称,但我不知道如何打印该值。注释掉的行会抛出错误:“无效的过程调用或参数”

Dim ContactsFolder As Folder
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
Dim Contact As ContactItem
Dim i As Integer

    For Each Contact In ContactsFolder.Items
        id = Contact.EntryID
        Debug.Print Contact.FirstName
        Debug.Print id
        For i = 0 To Contact.ItemProperties.Count - 1
            Debug.Print Contact.ItemProperties(i).Name
            'Debug.Print Contact.ItemProperties(i).Value
        Next
    Next

1 个答案:

答案 0 :(得分:0)

以下是抓取当前会话中所有地址列表的所有名称的示例。

您可以在https://msdn.microsoft.com/en-us/library/office/dn320232.aspx

找到该对象的所有属性

要获取对象的属性名称并对其进行评估,请参阅TypeLib引用。 (http://visualbasic.happycodings.com/applications-vba/code19.html可能适用也可能不适用)

Sub getContact()

    Dim ContactsFolder As Folder
    Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
    Dim Contact As ContactItem
    Dim i As Integer

    For Each Contact In ContactsFolder.Items
        ID = Contact.EntryID
        Debug.Print Contact.FirstName
        Debug.Print ID
        For i = 0 To Contact.ItemProperties.Count - 1
        Debug.Print Contact.ItemProperties(i).Name
        ' Doesn't work for object propertieties, like application.
        ' (Do Error Handling)
        Deubg.Print CallByName(Contact, Contact.ItemProperties(i).Name, VbGet) 
    Next
Next


End Sub