• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 923
  • Last Modified:

Loop with VBA through Outlook 2007 contacts stored on a different .pst

We have our contacts stored on a separate .pst which has nothing else other than contacts and the default stuff MS places on every pst. This is so we can synchronize out contacts here with a simple copy/paste script.

However, I can't seem to find a way to loop through the contacts using VBA for mass editing. I can loop through the default pst contacts, but those are empty.

Anyone knows how I can achieve this, please?
  • 2
1 Solution
David LeeCommented:
Hi, Cluskitt.

I'm assuming that you're familiar with VBA and using something like

Set olkFolder = Session.GetDefaultFolder(olFolderContacts)

Open in new window

to get the Contacts folder.  If so, then you can add this function to your code to get any folder via its path.

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
    End If
    On Error GoTo 0
End Function

Open in new window

To use this you'll call it like this

Set olkFolder = OpenOutlookFolder("Some Folder Path")

Open in new window

For example, if the folder is in a PST file called "Personal Folders" and is called "Contacts" then the path would be "Personal Folders\Contacts".
CluskittAuthor Commented:
I did stumble upon that piece of code, or something similar, but couldn't get it to work properly.

If I use the path (to continue with that example): "Personal Folders\Contacts\Contacts1" I do get an item count. But if I simply use "Personal Folders\Contacts", the item count is 0.

I want to loop all the folders inside contacts. Going through them one by one kinda defeats the purpose.

And yes, you can assume a decent VBA knowledge. :)
CluskittAuthor Commented:
Nevermind, got it working. I just had to make create another loop outside the items loop to cycle through the subfolders. Thanks for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now