Solved

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

Posted on 2013-05-31
3
892 Views
Last Modified: 2013-05-31
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?
0
Comment
Question by:Cluskitt
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 39210617
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
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        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
        Next
    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".
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 39210663
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. :)
0
 
LVL 18

Author Closing Comment

by:Cluskitt
ID: 39210692
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now