Change the "Item.To" value in Outlook when sending a message using VBA

I am trying to change the email address in the "Send" field in Outlook when the user clicks the submit button. for example, if the current value of Item.To = ' aaa@example.com ' becomes ' bbb@example.com ' .

I can change the topic, but failed with Item.To (is this a security issue?):

 Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) If Item.Class <> olMail Then Exit Sub Item.To = " bbb@example.com " ' Nope , It does not work Item.Subject = "New Subject" ' It works End Sub 

thanks

+4
source share
3 answers

The MailItem.To property MailItem.To used only for display names. You probably want to use the recipient collection, as in this slightly modified example from the Outlook help in the MailItem.Recipients property:

 Sub CreateStatusReportToBoss() Dim myItem As Outlook.MailItem Dim myRecipient As Outlook.Recipient Set myItem = Application.CreateItem(olMailItem) Set myRecipient = myItem.Recipients.Add(" bbb@example.com ") myItem.Subject = "New Subject" myItem.Display End Sub 
+5
source

I am the owner of the question. I chose @joeschwa's answer, but also want to display my code, which cancels the current message and creates a new one (you can change the recipients, message contents and something else):

 Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) If Item.Class <> olMail Then Exit Sub Dim newEm As String Dim Rec As Recipient Dim myItem As Outlook.MailItem Dim myRecipient As Outlook.Recipient Set myItem = Application.CreateItem(olMailItem) myItem.Body = Item.Body myItem.HTMLBody = Item.HTMLBody myItem.Subject = Item.Subject & " RASEEL PLUGIN " Cancel = True For Each Rec In Item.Recipients If InStr(1, Rec.AddressEntry, "@example.com", vbTextCompare) Then newEm = " example@example.net " Else newEm = Rec.AddressEntry End If Set myRecipient = myItem.Recipients.Add(newEm) myRecipient.Type = Rec.Type Next myItem.Send End Sub 
+4
source

This works for me. However, when changing the recipient, you must first delete the previous recipient. For instance,

x = .recipients.count if x = 1, then .recipients (1) .delete
.recipients.add " abc@dfg.com "

0
source

All Articles