Outlook 2010 GAL with Excel VBA

前端 未结 2 923
迷失自我
迷失自我 2020-12-20 06:34

I have the following code to get contacts out of Outlook from Excel:

Public Sub GetGAL()

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim          


        
相关标签:
2条回答
  • 2020-12-20 07:10

    Give this a try. Although if you have tons and tons of entries in your GAL, it will take awhile to complete, and you may have to increase the 65000.

    Sub tgr()
    
        Dim appOL As Object
        Dim oGAL As Object
        Dim oContact As Object
        Dim oUser As Object
        Dim arrUsers(1 To 65000, 1 To 2) As String
        Dim UserIndex As Long
        Dim i As Long
    
        Set appOL = CreateObject("Outlook.Application")
        Set oGAL = appOL.GetNameSpace("MAPI").AddressLists("Global Address List").AddressEntries
    
        For i = 1 To oGAL.Count
            Set oContact = oGAL.Item(i)
            If oContact.AddressEntryUserType = 0 Then
                Set oUser = oContact.GetExchangeUser
                If Len(oUser.lastname) > 0 Then
                    UserIndex = UserIndex + 1
                    arrUsers(UserIndex, 1) = oUser.Name
                    arrUsers(UserIndex, 2) = oUser.PrimarySMTPAddress
                End If
            End If
        Next i
    
        appOL.Quit
    
        If UserIndex > 0 Then
            Range("A2").Resize(UserIndex, UBound(arrUsers, 2)).Value = arrUsers
        End If
    
        Set appOL = Nothing
        Set oGAL = Nothing
        Set oContact = Nothing
        Set oUser = Nothing
        Erase arrUsers
    
    End Sub
    
    0 讨论(0)
  • 2020-12-20 07:22

    Your code assumes that you can only have ContactItem objects in the folder. It will break if you encounter an object of type DistListItem.

    Declare the item variable as a generic Object, then check the Type property or use TypeName function to figure out the exact item type.

    EDIT: PR_BUSINESS_ADDRESS_COUNTRY DASL name is

    http://schemas.microsoft.com/mapi/proptag/0x3A26001F 
    

    For address entries you can see the DALS property names in OutlookSpy. For example, you can click IMAPISession button, click QueryIdentity, select a property, look at the DASL edit box.

    0 讨论(0)
提交回复
热议问题