Consulting

Results 1 to 15 of 15

Thread: Change Subject on Outlook Mail and Forward

  1. #1

    Change Subject on Outlook Mail and Forward

    Hi,
    I need to create a script (I believe) to change the subject on all incoming email to "HC" and then forward to a separate email address. Can someone please help me? I have been looking online, but each VBA Script I try doesn't actually work when I put it into an Outlook rule.

    Thanks!

  2. #2
    Also, I am using outlook 2010 and POP mail.

    thanks,

  3. #3
    The following run from a rule should do the job

    Sub SendOnMessage(olItem As Outlook.MailItem)
    Dim olOutMail As Outlook.MailItem
        Set olOutMail = olItem.Forward
        With olOutMail
            .To = "someone@somewhere.com"
            .Subject = "HC"
            .sEnd
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Thanks!
    Can you also tell me how i can add specific words to the body? For example, I want to forward a message that comes in to my account on outlook and have the body of my forward be the from address of the email I'm forwarding.

    For example:
    User 1 (user1 [at] gmail) sends an email to me (me[at]gmail). Then on my account (using Outlook) i want to forward the email using the above script and also have the forward say in the body: Message was sent from: User1[at]gmail

    thanks,

  5. #5
    You will need a few extra lines for that:

    Sub SendOnMessage(olItem As Outlook.MailItem)
    Dim olOutMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
        Set olOutMail = olItem.Forward
        With olOutMail
            .To = "someone@somewhere.com"
            .Subject = "HC"
            .Display
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            oRng.Text = "Message was sent from: " & olItem.SenderEmailAddress
            .sEnd
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    Thank you so much! This is great. 1 more question.

    I am planning on having multiple email accounts come into my outlook and perform this script. I need to be able to create a new macro for each of the accounts, but how do i seperate them in the VBA? Does each get it's own module?

    Thanks!

  7. #7
    You only need the one script. When associated with a rule it acts on messages as they arrive that relate to the rule conditions.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    hi,
    my outlook rule that utilizes this VBA script keeps failing, and outlook throws an error about that rule failing. does anyone have any suggestions?

  9. #9
    the error shows run time error 91

  10. #10
    There is nothing contentious about the macro. It should not cause errors if entered as shown. The only line that needs to be changed is

    .To = "someone@somewhere.com", where you need to replace the address with an actual address (though this dummy address should not cause an error).

    Select a message and use the following macro to test the code and highlight where the error occurs.

    Sub Test()
    Dim olMsg As MailItem
        On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        SendOnMessage olMsg
    lbl_Exit:
        Set olMsg = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    Hi,
    No errors occur. any other advice?

    Thanks,

  12. #12

    error

    hi,
    seeing this errorimage1.jpg

  13. #13
    When I click Debug, it highlights this line:

    Set oRng = wdDoc.Range(0, 0)

    Thanks,

  14. #14
    Hmmmm. Replace that line with the following

    Set oRng = wdDoc.Range
    oRng.Collapse 1
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  15. #15
    works now! thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •