Link to home
Start Free TrialLog in
Avatar of rogerdjr
rogerdjrFlag for United States of America

asked on

How to Update a Outlook contact user field identifying those that had a read receipt

I am trying to Update a Outlook contact user field identifying those that had a read receipt

The code fails at varAdr = olkMsg.SenderEmailAddress, I think its because a return receipt is different from a regular email - but I'm stumped with how cto adapt and =internet searches were fruitless

Sub UpdateContactBasedOnFromEmailAddressEmail()
    Dim oOlApp As Outlook.Application
    Dim objNmSpc As NameSpace
    Dim ofldr As Object
    Dim ContFldr As Object
    Dim UpdateUserFieldYN As Integer
    Dim UpdtCount1 As Integer
    Dim olkMsg As Object, olkCon As Outlook.ContactItem, arrAdr As Variant, varAdr As Variant
   
    Set oOlApp = Outlook.Application
    Set objNmSpc = oOlApp.GetNamespace("MAPI")
   
    MsgBox "Select Email Folder"
    Set ofldr = objNmSpc.PickFolder
   
    MsgBox "Select Contacts Folder"
    Set ContFldr = objNmSpc.PickFolder

    MsgBox "Email Folder - " & ofldr & vbNewLine & "Contacts - " & ContFldr
   
    UpdateUserFieldYN = MsgBox("This Process Upadates User3 Field - Do You Want to Proceed?", vbYesNo)
   
    If UpdateUserFieldYN = 6 Then
        UpdtCount1 = 1
        For Each olkMsg In ofldr.Items
            If olkMsg.Class = olMail Or olkMsg.Class = 46 Then
                varAdr = olkMsg.SenderEmailAddress
'MsgBox varAdr & vbNewLine & olkMsg.Subject
                    Set olkCon = ContFldr.Items.Find("[Email1Address] = '" & varAdr & "'")
                    If TypeName(olkCon) = "ContactItem" Then
                        olkCon.User3 = "zzz - " & olkMsg.Subject
                        olkCon.Save
                    End If
           
                    UserForm1.TextBox1 = "Email " & varAdr & " " & olkMsg.Subject
                    UserForm1.TextBox2 = UpdtCount1
                    UserForm1.Show vbModeless
                    DoEvents
                               
                    UpdtCount1 = UpdtCount1 + 1
            End If
        Next
    End If
   
    Set olkCon = Nothing
    Set ContFldr = Nothing
    Set olkMsg = Nothing
       
    Unload UserForm1
       
    MsgBox "Macro Complete"
End Sub
Avatar of David Lee
David Lee
Flag of United States of America image

Hello again, rogerdjr.

Read receipts are of a class called ReportItem.  While they do not have a SenderEmailAddress property, they do in fact contain the address of the sender.  You just have to use the PropertyAccessor object to get that address.  I've added that functionality in the GetSenderSMTP function.  The only other change required was to implement a conditional statement to get the address from the SenderEmailAddress property if the item is an email or from the GetSenderSMTP function if the item is a receipt.  

I also removed the code that created an instance of Outlook's Application and NameSpace objects.  It's not necessary to create those in an Outlook macro.  The Application object and Session object are both already available, so creating another instance of them is unnecessary.

Sub UpdateContactBasedOnFromEmailAddressEmail()
    Dim ofldr As Object
    Dim ContFldr As Object
    Dim UpdateUserFieldYN As Integer
    Dim UpdtCount1 As Integer
    Dim olkMsg As Object, olkCon As Outlook.ContactItem, arrAdr As Variant, varAdr As Variant
      
    MsgBox "Select Email Folder"
    Set ofldr = Session.PickFolder
   
    MsgBox "Select Contacts Folder"
    Set ContFldr = Session.PickFolder

    MsgBox "Email Folder - " & ofldr & vbNewLine & "Contacts - " & ContFldr
   
    UpdateUserFieldYN = MsgBox("This Process Upadates User3 Field - Do You Want to Proceed?", vbYesNo)
   
    If UpdateUserFieldYN = 6 Then
        UpdtCount1 = 1
        For Each olkMsg In ofldr.Items
            If olkMsg.Class = olMail Or olkMsg.Class = olReport Then
                Select Case olkMsg.Class
                    Case olMail
                        varAdr = olkMsg.SenderEmailAddress
                    Case olReport
                        varAdr = GetSenderSMTP(olkMsg)
                End Select
'MsgBox varAdr & vbNewLine & olkMsg.Subject
                    Set olkCon = ContFldr.Items.Find("[Email1Address] = '" & varAdr & "'")
                    If TypeName(olkCon) = "ContactItem" Then
                        olkCon.User3 = "zzz - " & olkMsg.Subject
                        olkCon.Save
                    End If
           
                    UserForm1.TextBox1 = "Email " & varAdr & " " & olkMsg.Subject
                    UserForm1.TextBox2 = UpdtCount1
                    UserForm1.Show vbModeless
                    DoEvents
                               
                    UpdtCount1 = UpdtCount1 + 1
            End If
        Next
    End If
   
    Set olkCon = Nothing
    Set ContFldr = Nothing
    Set olkMsg = Nothing
       
    Unload UserForm1
       
    MsgBox "Macro Complete"
End Sub

Function GetSenderSMTP(olkMsg)
    Dim olkPA
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    MeetingSenderSMTP = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C1F001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Open in new window

Avatar of rogerdjr

ASKER

Thanks a million - I'm out of the office till Sat. - will test this on Sat.
Works great for the  emails that have the message class 43 - IPM.Note but skips over the messages with a message class 46 - Report.ipm.note.IPNRN or Report.ipm.note.IPNnRN
Is there a place I can learn about the message classes and how they function?
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Function and macro work perfectly now thanks a million

I tried to find something on line searching outlook 2013 message class - found this link but http://msdn.microsoft.com/en-us/library/office/ff861573(v=office.15).aspx could not find anything that tells me what a message class 43 or 46 mean - could you help with a location for a cross reference or a process in a message box that shows the actual message class like "IPM.Note"

I also found this website that shows how to expose message properties

Thanks again
I've requested that this question be closed as follows:

Accepted answer: 0 points for rogerdjr's comment #a40350356

for the following reason:

Great assistance - thanks alot
Sorry, but I have to object to closing the question without getting any credit when I clearly helped.
Sorry - my oversight I thought I accepted BlueDevilFan's solution and gave full credit to BlueDevilFan

HE WAS OF GREAT ASSISTANCE AND SHOULD GET FULL Credit