PDA

View Full Version : Object Vairable or With Block Variable not set



nolan
06-10-2011, 02:15 PM
Hi All,

I am getting the 'Object Vairable or With Block Variable not set' error mentioned in the subject line when I try to execute the following code and I'm not sure why. I'm new to Outlook VBA, so it's probably something obvious that I'm missing:


Sub GetGALContacts()
Dim outApp As Object, outNS As Outlook.NameSpace
Dim mtxAddressList As Outlook.AddressList
Dim mtxGALEntries As AddressEntries
Dim mtxGALEntry As AddressEntry
Dim thisContact As ContactItem
Set outApp = CreateObject("Outlook.Application")
Set outNS = outApp.GetNamespace("MAPI")
Set mtxAddressList = outNS.session.AddressLists("Global Address List")
Set mtxGALEntries = mtxAddressList.AddressEntries

For Each mtxGALEntry In mtxGALEntries
'Test if this record is from sync
Set thisContact = mtxGALEntry.GetContact
MsgBox thisContact.FirstName '<----**Error occurs here**
Next mtxGALEntry
Set outApp = Nothing
End Sub


Thanks in advance! Any help is really appreicated!

Nolan

Charlize
06-24-2011, 01:01 AM
Sub GetGALContacts()
Dim outNS As Outlook.NameSpace
Dim mtxAddressList As Outlook.AddressList
Dim mtxGALEntries As AddressEntries
Dim mtxGALEntry As AddressEntry
'Dim thisContact As ContactItem
'Not sure why you declare this, use mtxGALEntry for details

'I presume you are already in outlook and that it's active
Set outNS = Application.Session

Set mtxAddressList = outNS.Session.AddressLists("Global Address List")
Set mtxGALEntries = mtxAddressList.AddressEntries

For Each mtxGALEntry In mtxGALEntries
'Test if this record is from sync
MsgBox mtxGALEntry.DisplayType & " - " & mtxGALEntry.Name
'you can use with ... end with when you need more
'details from mtxGALEntry and store them in string variables
Next mtxGALEntry
End SubCharlize