Eem Jee
Eem Jee

Reputation: 1319

Import Contact/Nab Groups in outlook

I'm in this situation that I have to import all the contacts in outlook inside the organization including nab groups or group contacts. I have this code that I've found somewhere, but this not include the contact groups. This only imports the contact persons.

Sub Email_Extract()
Dim colAL As Outlook.AddressLists
Dim oAL As Outlook.AddressList
Dim colAE As Outlook.AddressEntries
Dim oAE As Outlook.AddressEntry
Dim oExUser As Outlook.ExchangeUser
Dim n As Long

Set colAL = Outlook.Application.Session.AddressLists

For Each oAL In colAL
StartTime = Timer

If oAL.AddressListType = olExchangeGlobalAddressList Then

Set colAE = oAL.AddressEntries
    n = 2
        For Each oAE In colAE

            If oAE.AddressEntryUserType = olExchangeUserAddressEntry Then

                Set oExUser = oAE.GetExchangeUser

                ThisWorkbook.Sheets("Sheet1").Cells(n, 1).Value = oExUser.Name 'User Name
                ThisWorkbook.Sheets("Sheet1").Cells(n, 2).Value = oExUser.PrimarySmtpAddress 'SMTP address
                n = n + 1
                Cells(n, 1).Activate
           End if
      Next
  Endif
Next
End sub

Please note that its run-time depends upon the email addresses of the organization. I've found some information here but the idea is a bit hanging. Is there anyway that I can include the contact groups on this process? Please help. Thanks.

Upvotes: 0

Views: 309

Answers (1)

niton
niton

Reputation: 9199

This is the hint there are other types so do not limit to one type.

If oAE.AddressEntryUserType = olExchangeUserAddressEntry Then

This demonstrates how you might process the other types. (The demo code is set up for Outlook not Excel.)

Option Explicit

Sub Email_Extract()

Dim colAL As Outlook.AddressLists
Dim oAL As Outlook.AddressList
Dim colAE As Outlook.AddressEntries
Dim oAE As Outlook.AddressEntry
Dim oExUser As Outlook.exchangeUser

Set colAL = Session.AddressLists

For Each oAL In colAL

    If oAL.AddressListType = olExchangeGlobalAddressList Then

        Set colAE = oAL.AddressEntries

        For Each oAE In colAE

            If oAE.AddressEntryUserType = olExchangeUserAddressEntry Then
                'Set oExUser = oAE.GetExchangeUser
                'Debug.Print oExUser.Name

            ElseIf oAE.AddressEntryUserType = olExchangeDistributionListAddressEntry Then
                ' https://msdn.microsoft.com/en-us/library/office/ff868214.aspx
                ' An address entry that is an Exchange distribution list.
                Debug.Print vbCr & "Exchange distribution list - AddressEntryUserType: " & oAE.AddressEntryUserType
                Debug.Print "  " & oAE.Name

            Else
                'Debug.Print vbCr & "? - AddressEntryUserType: " & oAE.AddressEntryUserType
                'Debug.Print "  " & oAE.Name

            End If
        Next
    End If
Next

End Sub

Upvotes: 1

Related Questions