Wednesday, April 18, 2012

Outlook Macro

We use a 3rd party product to integrate Salesforce.com with Outlook.  The client that we use is a add-in for Outlook and for contact integration, it relies on the contact's MessageClass property's value to be "IPM.Contact".  We've seen various products change the MessageClass from IPM.Contact to something else, like IPM.Contact.olTrans (for card scanners).  Blackberry will also change the MessageClass in some cases.  As a result, our add-in is not able to synch certain contacts with Salesforce.com.  To get around this, I created a small macro based on some sources I've seen online to flip the MessageClass and exposed the macro as a button within the Contact ribbon in Outlook 2010.  Below is the source code to change the MessageClass:




Sub Convert2Native()

Dim objApp As Outlook.Application
Dim objNS As NameSpace
Dim objSelection As Outlook.Selection
Dim objItem As ContactItem
Dim debugMode As String
Dim VarFileName As String


Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objSelection = objApp.ActiveExplorer.Selection

debugMode = "no"

'setup logging
VarFileName = "C:\OutlookConvert2NativeLog" & "_" & Year(Now) & "_" & Month(Now) & "_" & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & ".txt"
Open VarFileName For Append As #2
Print #2, "Opening logging"
 
Print #2, "debugMode = " & debugMode
 
MsgBox "Converting " & objSelection.Count
Print #2, "Converting " & objSelection.Count


If debugMode = "yes" Then
    For Each objItem In objSelection
        If objItem.MessageClass = "IPM.Contact" Then
            Print #2, "Converted Contact " & objItem.LastName & " from MessageClass " & objItem.MessageClass
            objItem.MessageClass = "IPM.Contact.olTrans"
            objItem.Save
        Else
            Print #2, "Did not convert Contact " & objItem.LastName & " from MessageClass " & objItem.MessageClass
        End If
    Next
Else
    For Each objItem In objSelection
        If objItem.MessageClass <> "IPM.Contact" And objItem.MessageClass <> "IPM.Contact.SD.Contact" And objItem.MessageClass <> "IPM.Contact.SD.Contact.Private" Then
            Print #2, "Converted Contact " & objItem.LastName & " from MessageClass " & objItem.MessageClass
            objItem.MessageClass = "IPM.Contact"
            objItem.Save
        Else
            Print #2, "Did not convert Contact " & objItem.LastName & " from MessageClass " & objItem.MessageClass
        End If
    Next
End If

Close #2

End Sub
-------------------
Sources (thank you!):
http://msdn.microsoft.com/en-us/library/ee814736.aspx

http://ideasmiths.wordpress.com/2007/07/17/solution-macro-program-to-change-some-fields-in-outlook-2007-contacts-in-bulk/