How to modify Outlook 2010 contacts field using VBA

We have Microsoft Outlook 2010.  We want modify the fields in Contacts using it's VBA.  How to go about it?
rayluvsAsked:
Who is Participating?
 
omgangIT ManagerCommented:
Give this a shot
OM Gang


Public Sub UpdateContacts()
On Error GoTo Err_UpdateContacts

    Dim olNS As NameSpace
    Dim olContactFolder As Folder, olMailBox As Folder
    Dim olContactItem As ContactItem
    Dim olItems As Outlook.Items
    Dim strMailboxName As String, strFolderName As String
    
    strMailboxName = "OM Gang"
    strFolderName = "Contacts"
       
    Set olNS = Application.GetNamespace("MAPI")
    Set olMailBox = olNS.Folders(strMailboxName)
    Set olContactFolder = olMailBox.Folders(strFolderName)
    Set olItems = olContactFolder.Items
    'Set olItems = olContactFolder.Items.Restrict("[MessageClass]='IPM.Contact'")
    
    For Each olContactItem In olItems
        'If olContactItem.CompanyName = "ABC Company" Then
            'do whatever you want to do here
        'End If
        Debug.Print olContactItem.LastName
    Next
    
Exit_UpdateContacts:
    Set olContactItem = Nothing
    Set olItems = Nothing
    Set olContactFolder = Nothing
    Set olNS = Nothing
    Exit Sub

Err_UpdateContacts:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_UpdateContacts

End Sub

Open in new window

0
 
omgangIT ManagerCommented:
Here's some sample code I have for deleting contacts.  Just about everything you need to get started.
OM Gang

Public Sub DeleteContacts()
On Error GoTo Err_DeleteContacts

    Dim olNS As NameSpace
    Dim olContactFolder As Folder
    Dim olContactItem As ContactItem
    Dim olItems As Outlook.Items
    Dim intCounter As Integer
    
    intCounter = 1
       
    Set olNS = Application.GetNamespace("MAPI")
    Set olContactFolder = olNS.GetDefaultFolder(olFolderContacts)
    Set olItems = olContactFolder.Items.Restrict("[MessageClass]='IPM.Contact'")
    
    For Each olContactItem In olItems
        olContactItem.Delete
        intCounter = intCounter + 1
        If intCounter = 2000 Then Exit For
    Next
    
Exit_DeleteContacts:
    Set olContactItem = Nothing
    Set olItems = Nothing
    Set olContactFolder = Nothing
    Set olNS = Nothing
    Exit Sub

Err_DeleteContacts:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_DeleteContacts

End Sub

Open in new window

0
 
omgangIT ManagerCommented:
Make a few changes so instead of deleting contacts you update them
OM Gang

Public Sub UpdateContacts()
On Error GoTo Err_UpdateContacts

    Dim olNS As NameSpace
    Dim olContactFolder As Folder
    Dim olContactItem As ContactItem
    Dim olItems As Outlook.Items
       
    Set olNS = Application.GetNamespace("MAPI")
    Set olContactFolder = olNS.GetDefaultFolder(olFolderContacts)
    Set olItems = olContactFolder.Items.Restrict("[MessageClass]='IPM.Contact'")
    
    For Each olContactItem In olItems
        If olContactItem.CompanyName = "ABC Company" Then
            'do whatever you want to do here
        End If

    Next
    
Exit_UpdateContacts:
    Set olContactItem = Nothing
    Set olItems = Nothing
    Set olContactFolder = Nothing
    Set olNS = Nothing
    Exit Sub

Err_UpdateContacts:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_UpdateContacts

End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
rayluvsAuthor Commented:
Just tried it but the control does not go thru "For Each olContactItem In olItems".  Seems that olItems has no value.
0
 
omgangIT ManagerCommented:
I just copied the procedure and pasted it into Outlook 2010 (ThisOutlookSession module) and made one change.  It works fine for me, i.e. it prints the last name of each contact to the Immediate Window.
OM Gang

Public Sub UpdateContacts()
On Error GoTo Err_UpdateContacts

    Dim olNS As NameSpace
    Dim olContactFolder As Folder
    Dim olContactItem As ContactItem
    Dim olItems As Outlook.Items
       
    Set olNS = Application.GetNamespace("MAPI")
    Set olContactFolder = olNS.GetDefaultFolder(olFolderContacts)
    Set olItems = olContactFolder.Items.Restrict("[MessageClass]='IPM.Contact'")
    
    For Each olContactItem In olItems
        'If olContactItem.CompanyName = "ABC Company" Then
            'do whatever you want to do here
        'End If
        Debug.Print olContactItem.LastName
    Next
    
Exit_UpdateContacts:
    Set olContactItem = Nothing
    Set olItems = Nothing
    Set olContactFolder = Nothing
    Set olNS = Nothing
    Exit Sub

Err_UpdateContacts:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_UpdateContacts

End Sub

Open in new window

0
 
omgangIT ManagerCommented:
Curious, in my sample code I explicitly limit the items to IPM.Contact items.  This should be the case for Contacts in your Contacts folder.  You can try switching the code to
    'Set olItems = olContactFolder.Items.Restrict("[MessageClass]='IPM.Contact'")
    Set olItems = olContactFolder.Items

to see if that makes a difference.  I get the same results either way.
OM Gang
0
 
rayluvsAuthor Commented:
same problem...  it jumpos the For/Next
0
 
omgangIT ManagerCommented:
Have you set a break point and stepped through the code?  You can see that the object variable olContactFolder is referencing the Outlook Contacts folder.
OM Gang

step through the procedure
0
 
rayluvsAuthor Commented:
Yes (i pasted exactly the same script). The "olContactFolder" has the value "Contacts", but "olItems" has nothing displayed (also tried with "Set olItems = olContactFolder.Items")
0
 
omgangIT ManagerCommented:
Are there contacts in the Contacts folder?  I know that sounds silly but it sure seems like the Contacts folder is empty.
OM Gang
0
 
omgangIT ManagerCommented:
I temporarily moved all contacts from my Contacts folder and am getting the same result as you - which is expected.  There is nothing to modify.
OM Gang
0
 
rayluvsAuthor Commented:
Don't worry, it's not silly, sometimes things are so easy, we miss it entirely.

But yes,  there is contacts.

But check this out, can this be culprit?

contacts
0
 
rayluvsAuthor Commented:
FYI:

Prior placing the question, we found varios example and gave us the same result; hence, we placed the question.

For example:
https://msdn.microsoft.com/en-us/library/dd673657(v=office.12).aspx
Dim objContactsFolder As Outlook.MAPIFolder
   Dim objContacts As Outlook.Items
   Dim strOldCo As String
   Dim strNewCo As String
   Dim objContact As Object
   Dim iCount As Integer

   ' Specify with which contact folder to work
   Set objContactsFolder = _
      Session.GetDefaultFolder(olFolderContacts)
   Set objContacts = objContactsFolder.Items

   ' Prompt for old and new company names
   strOldCo = InputBox("Enter the old company name.", _
      "Old Company Name")
   strNewCo = InputBox("Enter the new company name.", _
      "New Company Name")
   iCount = 0

   ' Process the changes
   For Each objContact In objContacts
      If TypeName(objContact) = "ContactItem" Then
         If objContact.CompanyName = strOldCo Then
            objContact.CompanyName = strNewCo
            objContact.Save
            iCount = iCount + 1
         End If
      End If
   Next

   ' Display the results
   MsgBox "Number of contacts updated:" & Str$(iCount), , _
      "ChangeCompanyName Finished"

   ' Clean up
   Set objContact = Nothing
   Set objContacts = Nothing
   Set objContactsFolder = Nothing

Open in new window


Gave same result, no data.  It jumps the For/Next.

So there must be a problem relating to the location of the Contacta?
0
 
omgangIT ManagerCommented:
Yes.  Thanks for the screen shot.  We are telling Outlook to get the default contacts folder.  It appears that the iCloud Contacts folder is what Outlook sees as the default.  We can change the line of code where we assign the Contacts folder object so that it explicitly selects the other Contacts folder.  At least I think we can; they both appear to be named the same but a little testing should provide a solution.

Unfortunately I have to leave for the day; I coach lacrosse and practice starts soon.  I will pick this back up tomorrow morning.
OM Gang
0
 
rayluvsAuthor Commented:
Ok, thanx!
0
 
rayluvsAuthor Commented:
Ok, we think we got it, just change to "Set olItems = Application.ActiveExplorer.CurrentFolder.Items".

It seems to work, we'll continue, but please give us your input in order to get it correctly.

Thanx!
0
 
omgangIT ManagerCommented:
That definitely works.  You just need to make sure the Contacts folder is currently selected before running the procedure, i.e. don't run the procedure while viewing the Inbox.

OM Gang
0
 
rayluvsAuthor Commented:
Do you know how to include a specific folder based on the pic below (our configuration):

olc
0
 
rayluvsAuthor Commented:
Gave error:

Error -2147221233, The attempted operation failed.  An object could not be found.
0
 
omgangIT ManagerCommented:
What values did you use for
strMailboxaName
strFolderName
??

Please post a screen shot of your main Outlook window navigation pane (left side).
OM Gang
0
 
omgangIT ManagerCommented:
On my system, I specified and secondary mailbox and the Contacts folder for that mailbox and the code worked correctly.
OM Gang
0
 
omgangIT ManagerCommented:
Based upon the screen shot you posted of your Contacts folders I'd think you should have specified

strMailboxName = "RR"
strFolderName = "Contacts"

or possibly
strMailboxName = "RR(All in Inbox)"

OM Gang
0
 
rayluvsAuthor Commented:
Sorry for the delay, you are correct, it was missing the correct name on strMailboxName & strFolderName:

   strMailboxName = "iCloud"
    strFolderName = "Contacts"


This was what worked.

Thanx a Lot, really!
0
 
rayluvsAuthor Commented:
Ok last thing, where can we find the filed name of the Contacts? (like Address, Telephone, Display As, etc.)
0
 
omgangIT ManagerCommented:
Use intellisense to see all properties that are available.
OM Gang

Contacts properties
0
 
rayluvsAuthor Commented:
Ok thanx
0
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.

All Courses

From novice to tech pro — start learning today.