问题
I need to automatically insert text into an outgoing email depending on the recipient. I found some code in an answer to another question (credit to 76Mel) that looks promising. It seems that I could attach the code to ItemSend in ThisOutlookSession
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.MessageClass = "IPM.Note" Then
For Each myRecipient In Item.Recipients
If myRecipient.Address = "<EMAIL ADDRESS TO FIND>" Then
<code to add text>
End If
Next
End If
End Sub
What would the code be that adds the text to the body of the email - and would this even work? Would this code fire before the email is sent, or is it already too late?
I do need it to be automated (creating a button or running the macro manually isn't really an option; it's a memory thing: if I could remember to run the macro, I could just add the text manually)
回答1:
Is this what you are trying? I have added the comments so you shouldn't have any problem understanding it :) If you still have a question, simply ask...
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'~~> Check if it is an email
If TypeName(Item) <> "MailItem" Then Exit Sub
Dim srchString As String, NewText As String
'~~> Email Address which you want to search for
srchString = "abc@gmail.com"
'~~> New text that you want to add
NewText = "Blah Blah"
'~~> Search To, CC, BCC Fields
If InStr(1, Item.To, srchString, vbTextCompare) Or _
InStr(1, Item.CC, srchString, vbTextCompare) Or _
InStr(1, Item.BCC, srchString, vbTextCompare) Then
'~~> Add the relevant text to the body
Item.Body = Item.Body & vbNewLine & NewText
End If
End Sub
I would recommend this MSDN Link.
Topic: MailItem Object Members
Link: http://msdn.microsoft.com/en-us/library/bb176688%28v=office.12%29.aspx
Quote From the Above Link
Represents a mail message in an Inbox folder.
Lists all Methods / Properties for a MailItem Object
来源:https://stackoverflow.com/questions/11264834/outlook-insert-text-based-on-recipient