Link userform to the outlook global address list

maramiro

Board Regular
Joined
Mar 17, 2009
Messages
67
Hello,
I have a userform and i m trying to link the contact dropbox of the userform to the outlook global adress list.
the following code doesnt give me the global adress list, it only provides me my personnel contact list.
Does anyone know how to twist the code to make it global instead of personnel.

Thank you
Code:
Private Sub UserForm_Initialize()
'Outlook objects.
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olConItems As Outlook.Items
Dim olItem As Object

'Excel objects.
Dim wbBook As Workbook
Dim wsSheet As Worksheet

'Location in the imported contact list.
Dim lnContactCount As Long
Dim strDummy As String
'Turn off screen updating.
Application.ScreenUpdating = False
'Initialize the Excel objects.
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Sheets("phonebook")

'Format the target worksheet.
With wsSheet
.Range("A1").CurrentRegion.Clear
.Cells(1, 1).Value = "Name"
.Cells(1, 2).Value = "Title"
With .Range("A1:F1")
.Font.Bold = True
.Font.ColorIndex = 10
.Font.Size = 11
End With
End With
wsSheet.Activate
'Initalize the Outlook variables with the MAPI namespace and the default Outlook folder of the current user.
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(10)
Set olConItems = olFolder.Items

'Row number to place the new information on; starts at 2 to avoid overwriting the header
lnContactCount = 2
'For each contact: if it is a business contact, write out the business info in the Excel worksheet;
For Each olItem In olConItems
If TypeName(olItem) = "ContactItem" Then
With olItem
If InStr(olItem.CompanyName, strDummy) > 0 Then
Cells(lnContactCount, 1).Value = .LastNameAndFirstName
Cells(lnContactCount, 2).Value = .JobTitle

End If
End With
lnContactCount = lnContactCount + 1
End If
Next olItem
'Null out the variables.
Set olItem = Nothing
Set olConItems = Nothing
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
'Sort the rows alphabetically using the CompanyName or FullName as appropriate, and then autofit.
With wsSheet
.Range("A2", Cells(2, 6).End(xlDown)).Sort key1:=Range("A2"), order1:=xlAscending
.Range("A:B").EntireColumn.AutoFit
End With
'Turn screen updating back on.
Application.ScreenUpdating = True
Worksheets("MENU").Select

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I had inserted a break on the for loop, but nothing shows other than the userform.
Even if i put breaks all over the code, nothing shows other than the userform. In other words, i dont see the breaks getting "highlighted" yellow.
 
Upvote 0
If you have a GAL you should get a popup just like you do in Outlook. Does this standalone macro work for you?

Code:
Sub Test()
    Dim cdoSession, cdoAddressBook, olkRecipients, objAE
    On Error Resume Next
    Set cdoSession = CreateObject("MAPI.Session")
'   Change the name of your Outlook profile as needed.
    cdoSession.Logon "", "", False, False
    Set olkRecipients = cdoSession.AddressBook(, "Global Address List", 0, False)
    For Each objAE In olkRecipients
        MsgBox objAE.Name
    Next
    Set olkRecipients = Nothing
    cdoSession.Logoff
    Set cdoSession = Nothing
End Sub
 
Upvote 0
no, still not working.
I had inserted the code in a module by itself.
and clicked on the textbox of the userform. Something is just weird, if it works with you and not me.
 
Upvote 0
how about the code i had posted at the begining?
It works but the only thing is it gives me my personnel contact and not my GAL contact.
 
Upvote 0
I commented out the line above. and it gave me an error saying "" Run-time error '429': Activex component cant create object".
I had copy paste the code in a new module not in the userform module.
it is also a new workbook that i had created a userform for from skratch.


and the following line is highlighted yellow
Code:
Set cdoSession = CreateObject("MAPI.Session")
 
Upvote 0

Forum statistics

Threads
1,215,581
Messages
6,125,656
Members
449,247
Latest member
wingedshoes

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