Consulting

Results 1 to 5 of 5

Thread: Help - Outlook 2010 VBA script - Can't find Outlook contacts folder in object model

  1. #1
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    3
    Location

    Help - Outlook 2010 VBA script - Can't find Outlook contacts folder in object model

    Using VBA macro scripting (Outlook 2010 on Windows 7) I am trying to programatically repair corrupted telephone numbers in an Outlook contacts subfolder (phone numbers appear duplicated in scientific notation rather than telephone formatting). This is my first time working with the Outlook Contacts object model and am having trouble finding (and binding to) the corrupted folder. This is how my local Contacts hierarchy appears in the folder explorer:

    MyContacts
    Contacts - University Outlook365 account < Primary email but no contact entries (don't use) Contacts - Private PST account < My primary contact list (lots of entries) Suggested Contacts - University Outlook365 < Historical email address only Suggested Contacts - Private PST account < Historical email address only Suggested Contacts - Private website A < No contacts Suggested Contacts - Private website B < No contacts Search results
    My university Outlook 365 address is what I primarily use for outgoing and incoming email, but for privacy purposes I use the local PST folder to store contact information. This is the corrupted contact folder (highlighted in bold) and the one I am trying to access and repair.

    I have been trying to bind to this folder using various VBA scripts, like the following, but no contact items are found. I suspect that my non-standard and hybrid O365/PST folder organization and inexperience with the Outlook object model may be to blame, but would appreciate any assistance or guidance from forum members with more experience in this area.

    Sub GetContactsFolder()
    
    Dim olNS As Outlook.NameSpace
    Dim xolContactFolder As Outlook.MAPIFolder
    Dim olContact As Outlook.ContactItem
    Dim olItems As Outlook.Items
    
    
    Set olNS = Application.GetNamespace("MAPI")
    Set xolContactFolder = olNS.GetDefaultFolder(olFolderContacts).Parent.Folders("Private PST account")
    Set olItems = xolContactFolder.Items
    
    
    For Each olContact In olItems
    
    
       'repair corrupted contacts
        Debug.Print olContact.FullName
    
    
    Next
    
    
    Set olItems = Nothing
    Set olNS = Nothing
    Set olContactFolder = Nothing
    Set olContact = Nothing
    
    
    End Sub
    * Note that the account folder names shown above are descriptive and fictitious for privacy

    In the above script, olItems.count is zero.
    Last edited by BGood; 06-26-2019 at 10:49 AM. Reason: typos

  2. #2
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    3
    Location
    I am still looking for a repair solution using VBA scripting from within Outlook, but getting dizzy from trying to find/bind to my corrupted folder through the Outlook object model. As a simpler alternative which circumvents the complexities of Outlook's Object Model, I was able to export the corrupted contacts folder to a csv format file and make the necessary programmatic repairs using VBA scripting in Excel, a much more familiar environment for me.

    Because Excel csv files cannot contain macros, the csv export file from Outlook was loaded into Excel and saved as an .xls workbook with macros explicitly enabled under Options>Advanced>Security Trust. After I make another backup export of the corrupt folder, I will save the Excel .xls worksheet in csv format and import it to Outlook. If anyone sees anything wrong with this plan, please give me a heads-up.

    Also, if anyone sees what I am doing wrong in finding/binding to the Outlook folder in Outlook VBA, please let me know.

  3. #3
    You probably need to modify the code as follows
    Sub GetContactsFolder()
    
    Dim olNS As Outlook.NameSpace
    Dim olStore As Outlook.Store
    Dim olContactFolder As Outlook.MAPIFolder
    Dim olFolder As Outlook.MAPIFolder
    Dim olContact As Object
    Dim iContact As Integer
    
    
        Set olNS = Application.GetNamespace("MAPI")
        Set olContactFolder = olNS.GetDefaultFolder(olFolderContacts)
        For Each olFolder In olContactFolder.folders
            'Debug.Print olFolder.Name
            If olFolder.Name = "Private PST account" Then
                For Each olContact In olFolder.Items
                    If TypeName(olContact) = "DistListItem" Then
    Debug.Print olContact.DLName
                    Else
    Debug.Print olContact.FullName
                    End If
                Next olContact
                Exit For
            End If
        Next olFolder
        Set olNS = Nothing
        Set olContactFolder = Nothing
        Set olFolder = Nothing
        Set olContact = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    3
    Location
    Thanks for the reply, Graham. I executed your code, uncommenting the debug.print olFolder.Name statement and here is the immediate output:

    olFolder: Recipient CacheolFolder: PeopleCentricConversation Buddies
    olFolder: Organizational Contacts
    olFolder: GAL Contacts
    olFolder: Companies
    olFolder: {A9E2BC46-B3A0-4243-B315-60D991004455}
    olFolder: {06967759-274D-40B2-A3EB-D7F9E73727D7}
    It seems like the target folder is not found within the folders returned by olNS.GetDefaultFolder(olFolderContacts). Seems strange.

  5. #5
    Try looping the parent folders also and see what that gives you.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •