Outlook - insert text based on recipient

大城市里の小女人 提交于 2019-12-10 21:21:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!