Importing Outlook Global Address List into Excel 2007

ttratl

Board Regular
Joined
Dec 21, 2004
Messages
168
Hi everyone,

I've been happily using this code to get our GAL into Excel 2003 for a couple of years, but now we're on 2007, it errors on this line:

Dim objSession As MAPI.Session

Would anyone know how to change this so it works with Office 2007 please?

Code:
Option Explicit
Const CdoAddressListGAL = 0
Const CdoUser = 0
Const CdoRemoteUser = 6
#Const EarlyBind = True
 
Sub GetGAL()
          
    Dim x As Variant, CDOList As Variant, TitleList As Variant, CDOitem As Variant
    Dim NumX As Long, ArrayDump As Long, i As Long, v As Long, u As Long
     
     'Change the #Const to True to enable Early Binding
     
    #If EarlyBind Then
        Dim objSession As MAPI.Session, oFolder As MAPI.AddressList, oMessage As MAPI.AddressEntry
        Set objSession = New MAPI.Session
        CDOList = Array(CdoPR_DISPLAY_NAME, CdoPR_GIVEN_NAME, CdoPR_SURNAME, 972947486, CdoPR_ACCOUNT, _
        CdoPR_TITLE, CdoPR_OFFICE_TELEPHONE_NUMBER, CdoPR_MOBILE_TELEPHONE_NUMBER, CdoPR_PRIMARY_FAX_NUMBER, _
        CdoPR_COMPANY_NAME, CdoPR_DEPARTMENT_NAME, 974716958, CdoPR_STREET_ADDRESS, _
        CdoPR_LOCALITY, CdoPR_STATE_OR_PROVINCE, CdoPR_COUNTRY, _
        CdoPR_ASSISTANT, CdoPR_ASSISTANT_TELEPHONE_NUMBER)
    #Else
        Dim objSession As Object, oFolder As Object, oMessage As Object
        Set objSession = CreateObject("MAPI.Session")
        CDOList = Array(805371934, 973471774, 974192670, 972947486, 973078558, 974585886, _
        973602846, 974913566, 975372318, 974520350, 974651422, 974716958, 975765534, _
        975634462, 975699998, 975568926, 976224286, 976093214)
    #End If
     
    With objSession
        .Logon , , False, False
        Set oFolder = .GetAddressList(CdoAddressListGAL)
    End With
     
    TitleList = Array("GAL Name", "Given Name", "Surname", "Email address", "Logon", "Title Field", _
    "Telephone", "Mobile", "Fax", "CSG/Group", "Department", "Site", "Address", "Location", "State ", _
    "Country Field", "Assistant Name", "Assistant Phone")
     
     'Grab 2000 records in one hit before writing to sheet
     
    ArrayDump = 2000
    Cells.Clear
     
     'Add Titles
    With Range("A1").Resize(1, UBound(TitleList) + 1)
        .Formula = TitleList
        .HorizontalAlignment = xlCenter
        .Interior.ColorIndex = 35
        .Font.Bold = True
        .Font.Size = 12
    End With
     
    ReDim x(1 To ArrayDump, 1 To UBound(CDOList) + 1)
     
    On Error Resume Next
     'Some fields may not exist
     
     'Turn off screen updating
    Application.ScreenUpdating = False
    For Each oMessage In oFolder.AddressEntries
         
        Select Case oMessage.DisplayType
        Case CdoUser, CdoRemoteUser
            i = i + 1
             'Reset variant array every after each group of records
            If i Mod (ArrayDump + 1) = 0 Then
                 
                 'Check that records do notexceed one sheet
                If NumX * ArrayDump + i > 65535 Then
                    MsgBox "GAL exceeds 65535 entries - extraction stopped ", vbCritical + vbOKOnly
                    GoTo FastExit
                End If
                 
                 'Dump data
                NumX = NumX + 1
                Range("A2").Offset((NumX - 1) * ArrayDump, 0).Resize(ArrayDump, UBound(CDOList) + 1) = x
                ReDim x(1 To ArrayDump, 1 To UBound(CDOList) + 1)
                 
                i = 1
            End If
             'Display status to user
            If i Mod ArrayDump = 0 Then
                Application.StatusBar = "Entry " & i + u + NumX * ArrayDump & " of " & oFolder.AddressEntries.Count
                DoEvents
            End If
             
            v = 0
             ' Add detail to each address
            For Each CDOitem In CDOList
                v = v + 1
                x(i, v) = oMessage.Fields(CDOitem)
            Next
        Case Else
            u = u + 1
        End Select
    Next
     
     'dump remaining entries
    Range("A2").Offset(NumX * ArrayDump, 0).Resize(ArrayDump, UBound(CDOList) + 1) = x
     
     'cleanup
FastExit:
    ActiveSheet.UsedRange.EntireRow.WrapText = False
    Cells.EntireColumn.AutoFit
     
    Application.StatusBar = ""
    Application.ScreenUpdating = True
     
    Set oFolder = Nothing
    Set objSession = Nothing
     
End Sub

I've no idea how this code works, or what it does, only that it's worked perfectly for years...

Thanks.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Either change #Const EarlyBind = True to #Const EarlyBind = False, or set a reference to MS CDO Library (Tools - References in VB editor).
 
Upvote 0
When I change #Const EarlyBind = False it errors at
Code:
Set objSession = CreateObject("MAPI.Session")

I'm not sure how to set a reference. I can't find "MS CDO Library" in the Tools, References list. The nearest is Microsoft CDO for Exchange 2000 library?
 
Upvote 0
Upvote 0
John,
I have Microsoft CD0 1.21 added, but I am still get nowhere with the code mentioned above. And as pointed out, in works perfectly well on my 2003 system.
I have been tearing my hair out for the last 3 days to resolve this issue.
thanks!
 
Upvote 0
Just an update on my problem - my issue is on a work PC. Our IT dept were unable to install CDO 1.21.
We tried this one: http://www.microsoft.com/downloads/...31-079A-43A9-BFF2-0A110307611E&displaylang=en
which has the MAPI client, which is also referred to in the code, so figured we'd need it...

We have now converted over to Exchange 2007, so even if we had got CDO installed, IT say it still wouldn't work.

My very long-winded work around is to use MS Access 2002, which I have on this PC. However Exchange 2007 doesn't seem to throw out SMTP email addesses, so lots of code is needed to manipulate the exported Access database, to get a simple & useful GAL list.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top