3
votes

I am trying to access the .To line from an Outlook-Mail I clicked on in Outlook.

Specifically I want to check whether the mail has been sent to a specific email.

Wider view:
Let's say, I click on the mail and my subroutine found out that the mail has been sent to this specific email address(Item.To field). In this case, If the users chooses to reply to that mail (reply-event), the CC-field of the reply-mail should automatically contain another email address the mail will be send to.

What I got so far and isn't working:

Private Sub Application_ItemLoad(ByVal Item As Object)
    MsgBox "To: " & Item.To
End Sub

Error: Values and methods of the element could not be handled in this event.

Do I maybe need to cast the object somehow to declare it as a mailitem-object to access the .To field? Or any other suggestions that might work?

2
Have you tried my code? I'd appreciate it if you could let me know if it worked.JimmyPena

2 Answers

1
votes

The Application ItemLoad event happens when the Item has not been fully initialized. It is useful to hook up another Item Event, such as Open, Read (probably the one you want), etc. We wold accomplish the Event you want with the code below, on the "ThisOutlookSession" class:

Public WithEvents myItem As Outlook.mailItem

Private Sub Application_ItemLoad(ByVal Item As Object)
    If (TypeOf Item Is mailItem) Then
        Set myItem = Item
    End If
End Sub

Private Sub myItem_Read()
 ...
End Sub

Cheers!

0
votes

Application_ItemLoad was introduced in Outlook 2007, so I assume that is your version.

I am not able to test this but it should work:

Sub CheckRecips()

Dim msg As Outlook.mailItem
Dim msgRecips As Outlook.Recipients
Dim msgRecip As Outlook.Recipient
Dim msgReply As Outlook.mailItem
Dim found As Boolean
Dim replyRecip As Outlook.Recipient

If TypeName(Outlook.ActiveExplorer.Selection(1)) = "MailItem" Then
  Set msg = Outlook.ActiveExplorer.Selection(1)
  Set msgRecips = msg.Recipients

  For Each msgRecip In msgRecips
    If msgRecip.Type = olTo And msgRecip.Address = "email address you're looking for" Then
      ' we found it, exit loop
      found = True
      Exit For
    End If
  Next msgRecip

    ' create reply
  Set msgReply = msg.Reply

  ' if email address was on recipient list, CC someone else on reply
  If found Then
      Set replyRecip = msgReply.Recipients.Add("someone else who should be CC'd")
      replyRecip.Type = olCC
  End If

  msgReply.Display
End If

End Sub

Replace "email address you're looking for" with the email address of the "specific" person who may have received the email.

Replace "someone else who should be CC'd" with the email address of the person you want to CC when that specific person gets the message.

Assign this to a toolbar button and use it whenever you need to.