Monday, March 14, 2011

Export GAL contacts using GetExchangeUser

Was searching the net for a neat and easy way to export GAL contacts from Outlook. There were many options that exported contacts but I couldnt find one that pulled the Exchange Server properties for the user.
Obviously VBA seemed like the most recommended option so I had some exploring to do.

Enter the GetExchangeUser method which is available in Office 2007


  1. Create a Email message with the list of contacts you want to export and save it as a Draft

  2. Go to the Draft foldert and select the email message

  3. Enter the VBA mode in Outlook and create a new module, paste the below code and execute


  4. Sub AddToContactsFromEmail()
        Dim olkContacts As MAPIFolder, olkContact As ContactItem, olkSelected As Selection, olkItem As Object, olkRecipient As Recipient
        'Select the email in outlook from which you want the contacts added
        Set olkSelected = Application.ActiveExplorer.Selection
        If olkSelected.Count > 0 Then
            For Each olkItem In olkSelected
                If olkItem.Class = olMail Then
                    Set olkContacts = Application.Session.GetDefaultFolder(olFolderContacts)
                    For Each olkRecipient In olkItem.Recipients
                        Set olkContact = Application.CreateItem(olContactItem)
                        With olkContact
                            .FullName = olkRecipient.Name
                            .Email1Address = olkRecipient.Address
                            .BusinessAddress = olkRecipient.AddressEntry.GetExchangeUser.Address
                            .OfficeLocation = olkRecipient.AddressEntry.GetExchangeUser.OfficeLocation
                            .BusinessTelephoneNumber = olkRecipient.AddressEntry.GetExchangeUser.BusinessTelephoneNumber
                            .JobTitle = olkRecipient.AddressEntry.GetExchangeUser.Alias
                            'Enter additional properties required from GAL
                            .Categories = "Exported"
                            .Save
                        End With
                    Next
                End If
            Next
        End If
        Set olkContact = Nothing
        Set olkContacts = Nothing
        Set olkSelected = Nothing
        Set olkItem = Nothing
        Set olkRecipient = Nothing
    End Sub
    


  5. You will see all contacts in your email message exported to the default Contacts folder in Outlook with the properties you specified in the code

  6. From here you can just use the Import/Export feature in outlook to export the Contact list to excel.



Note: There is cvsde utility that exists apparently in the Windows Server that helps you export GAL but I wasnt able to locate the utility on my client machine.

No comments:

Post a Comment