move through a public folder of outlook contacts and read names, RRS feed

  • Question

  • In Access 2007  VBA I want to move through a public folder of outlook contacts and  read names, email addresses and other fields of my choice into a table. The table work is easy for me but I’m getting nowhere with the outlook code.

    I’ve tested code from all over the web. 

    This code blows up on “session” in  "Application.Session"

    Dim Session As Outlook.NameSpace

        Dim Report As String

        Dim ContactFolder As Outlook.Folder

        Dim currentItem As Object

        Dim currentContact As ContactItem

        Set Session = Application.Session

    I’ve understood this is due to the code, in fact, being VB and not VBA.

    Can anyone point me to working VBA code or any kind of complete working documentation ? 

    Friday, May 17, 2013 10:57 PM

All replies

  • If your code is running in Excel VBA, the Application intrinsic variable points to the Excel.Application object.

    If you need Outlook.Application, you need to rename the variable to avoid name collision and explicitly initialize it:

    Dim OlApp as Outlook.Applicationset 
    OlApp = CreateObject("Outlook.Application")

    You will also need to make sure Outlook has a valid MAPi session, otherwise Application.Session can be null. Instead of using Set Session = Application.Session, use

    set Session = olApp.GetNamespace("MAPI")

    Dmitry Streblechenko (MVP)
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, May 17, 2013 11:05 PM
  • Thanks for the help, but I could never keep the object model straight in my head, and so I cant use your suggestions; I have NO creative  capacity here. The only way I can ever get anywhere with outlook (I try to avoid projects using it !) is to play with complete working code. 

    I did find this from a project of mine 6 years ago; it does not blow up but I cant advance it either.I just want to grab a few of the fields to debug display them at the moment. If Sue Mosher is around; help !

      Dim ol As New Outlook.Application
       Dim olns As Outlook.NameSpace
       Dim cf As Outlook.MAPIFolder
       Dim c As Outlook.ContactItem
       Dim Prop As Outlook.UserProperty

       Set olns = ol.GetNamespace("MAPI")
       Set cf = olns.GetDefaultFolder(olFolderContacts)
    Debug.Print cf
     Debug.Print c.CompanyName

     'For Each objContactItem In cf.Items
    '    Debug.Print objContactItem
     '   Debug.Print c.CompanyName
    '  Next


        Set olns = Nothing
        Set cf = Nothing

    Sunday, May 19, 2013 3:43 PM