Problem retireving Outlook contacts

G

Guest

Guest
This is driving me crazy. As I see it the following code snippet should list everyone in my Outlook Contacts folder. It almost does, it seems to miss the newest entries. I'm sure it's something simple, any suggestions?

...
Dim olapp As Outlook.Application
Dim nspNameSpace As Outlook.NameSpace
Dim fldContacts As Outlook.MAPIFolder
Dim objContacts As Object
Dim objContact As Object
Dim iRow As Integer
Dim sName As String

On Error GoTo Err_Handler

Set olapp = CreateObject("Outlook.Application")
Set nspNameSpace = olapp.GetNamespace("MAPI")
Set fldContacts = nspNameSpace.GetDefaultFolder(olFolderContacts)
Set objContacts = fldContacts.Items
iRow = 1
For Each objContact In objContacts
sName = objContact.FileAs
Cells(iRow, 1).Value = sName
Next objContact
...
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I've just tried your code (slightly modified) and it works fine, even if I add a new contact and then run it again.

Sub GetOutlookContacts()

Dim olapp As Outlook.Application
Dim nspNameSpace As Outlook.NameSpace
Dim fldContacts As Outlook.MAPIFolder
Dim objContacts As Object
Dim objContact As Object
Dim iRow As Integer
Dim sName As String

On Error GoTo Err_Handler

Set olapp = CreateObject("Outlook.Application")
Set nspNameSpace = olapp.GetNamespace("MAPI")
Set fldContacts = nspNameSpace.GetDefaultFolder(olFolderContacts)
Set objContacts = fldContacts.Items
iRow = 1
For Each objContact In objContacts
sName = objContact.FileAs
Cells(iRow, 1).Value = sName
iRow = iRow + 1
Next objContact

Exit Sub

Err_Handler:

MsgBox Err.Description

End Sub


HTH,
D
 
Upvote 0
Thanks, My Bad... I'd messed up the in the error handling routine.

Thanks for the help. Tony
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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