Log in

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



BGood
06-26-2019, 10:46 AM
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.

BGood
06-26-2019, 03:56 PM
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.

gmayor
06-26-2019, 09:41 PM
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

BGood
06-27-2019, 07:03 AM
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.

gmayor
06-27-2019, 08:23 PM
Try looping the parent folders also and see what that gives you.