none
Reading contacts from Outlook 2013 shared folder RRS feed

  • Question

  • Hi all,

    A year ago I developed an Access 2010 app which would read contacts from a shared Outlook 2010 contacts folder and write them to a table (for performing SQL queries elsewhere in the app).

    This is the code:

    Set olApp = CreateObject("Outlook.Application")
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.Folders("Public Folders - " & olApp.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress).Folders("Favorites").Folders("Acme Shared Contacts")
    
    For Each olCi In Fldr.Items
        coname = olCi.CompanyName
        email = olCi.Email1Address
        FirstName = olCi.FirstName
        LastName = olCi.LastName
    
        DoCmd.SetWarnings False
        DoCmd.RunSQL "INSERT INTO Contacts ([Company], [E-mail address], [First], [Last]) VALUES ('" & coname & "', '" & email & "', '" & FirstName & "', '" & LastName & "');"
        DoCmd.SetWarnings True
        
    Next olCi

    The client has now upgraded to Access 2013 and Outlook 2013, AND moved the location of the folders.  The new folder location looks like "Other Contacts \ Shared Contacts" (i.e. a folder called "Shared Contacts" under "Other Contacts".)  The properties of that folder are "\\Public Folders - username@acmie.com"

    I tried simply changing 

    Set Fldr = olNs.Folders("Public Folders - " & olApp.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress).Folders("Favorites").Folders("Acme Shared Contacts")

    To

    Set Fldr = olNs.Folders("Public Folders - " & olApp.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress).Folders("Shared Contacts").Folders("Other Contacts")

    But get the error:

    "The attempted operation failed.  An object could not be found"

    Any ideas?


    • Edited by RobDog63 Tuesday, March 25, 2014 1:21 PM
    Tuesday, March 25, 2014 1:20 PM

Answers

  • Hello Rob,

    First of all, I'd recommend breaking the chain of calls into separate lines. Thus, you may find the problematic property or method.

    It looks like you are interested in the GetSharedDefaultFolder method of the Namespace class. It returns a Folder object that represents the specified default folder for the specified user. MSDN provides the following sample code:

    Sub ResolveName()  
     Dim myNamespace As Outlook.NameSpace  
     Dim myRecipient As Outlook.Recipient  
     Dim CalendarFolder As Outlook.Folder 
     
     Set myNamespace = Application.GetNamespace("MAPI")  
     Set myRecipient = myNamespace.CreateRecipient("Dan Wilson")  
     myRecipient.Resolve  
     If myRecipient.Resolved Then  
     Call ShowCalendar(myNamespace, myRecipient)  
     End If  
    End Sub 
       
    Sub ShowCalendar(myNamespace, myRecipient)  
     Dim CalendarFolder As Outlook.Folder 
     
     Set CalendarFolder = _  
     myNamespace.GetSharedDefaultFolder _  
     (myRecipient, olFolderCalendar)  
     CalendarFolder.Display  
    End Sub
    

    Tuesday, March 25, 2014 2:00 PM
  • It's never a good idea to use compound dot operators, it makes it impossible to find out where the code is failing. Separate each object instantiation into a separate line so you can see what's failing.

    To get the public folders the best way is to use the NameSpace.GetDefaultFolder(olPublicFoldersAllPublicFolders) method. That returns the root folder of the public folders. From there you can navigate to the desired folder.

    Set Fldr = olNS.GetDefaultFolder(olPublicFoldersAllPublicFolders)

    Set Fldr1 = Fldr.Folders("Shared Contacts")

    Set Fldr2 = Fldr1.Folders("Other Contacts")

    This way you can step the code and see where it's failing and what's not being set. This way also gives you the public folders root in an agnostic way that is language and Exchange version independent.


    Ken Slovak MVP - Outlook

    Tuesday, March 25, 2014 2:01 PM
    Moderator
  • Thanks guys, that helped me pin it down to the correct folder.

    Rob

    • Marked as answer by RobDog63 Tuesday, March 25, 2014 5:34 PM
    Tuesday, March 25, 2014 3:09 PM

All replies

  • Hello Rob,

    First of all, I'd recommend breaking the chain of calls into separate lines. Thus, you may find the problematic property or method.

    It looks like you are interested in the GetSharedDefaultFolder method of the Namespace class. It returns a Folder object that represents the specified default folder for the specified user. MSDN provides the following sample code:

    Sub ResolveName()  
     Dim myNamespace As Outlook.NameSpace  
     Dim myRecipient As Outlook.Recipient  
     Dim CalendarFolder As Outlook.Folder 
     
     Set myNamespace = Application.GetNamespace("MAPI")  
     Set myRecipient = myNamespace.CreateRecipient("Dan Wilson")  
     myRecipient.Resolve  
     If myRecipient.Resolved Then  
     Call ShowCalendar(myNamespace, myRecipient)  
     End If  
    End Sub 
       
    Sub ShowCalendar(myNamespace, myRecipient)  
     Dim CalendarFolder As Outlook.Folder 
     
     Set CalendarFolder = _  
     myNamespace.GetSharedDefaultFolder _  
     (myRecipient, olFolderCalendar)  
     CalendarFolder.Display  
    End Sub
    

    Tuesday, March 25, 2014 2:00 PM
  • It's never a good idea to use compound dot operators, it makes it impossible to find out where the code is failing. Separate each object instantiation into a separate line so you can see what's failing.

    To get the public folders the best way is to use the NameSpace.GetDefaultFolder(olPublicFoldersAllPublicFolders) method. That returns the root folder of the public folders. From there you can navigate to the desired folder.

    Set Fldr = olNS.GetDefaultFolder(olPublicFoldersAllPublicFolders)

    Set Fldr1 = Fldr.Folders("Shared Contacts")

    Set Fldr2 = Fldr1.Folders("Other Contacts")

    This way you can step the code and see where it's failing and what's not being set. This way also gives you the public folders root in an agnostic way that is language and Exchange version independent.


    Ken Slovak MVP - Outlook

    Tuesday, March 25, 2014 2:01 PM
    Moderator
  • Thanks guys, that helped me pin it down to the correct folder.

    Rob

    • Marked as answer by RobDog63 Tuesday, March 25, 2014 5:34 PM
    Tuesday, March 25, 2014 3:09 PM