Run Script to Append Subject and Body of Email

3.9k views Asked by At

I am [attempting to] learn how to write a script in Outlook that when a certain category is set on an email:

  1. Append the Subject with " PROJ=5"
  2. Append the Body with about 10 lines of text
  3. Send email.

My goal is to mark an email with a category and forward the email to our ticketing system.

I'm not really having any luck with the samples I have found.

Samples (URL) I have tried (Copied code and updated relevant fields):

1

There are 1 answers

4
0m3r On
  1. Append the Subject with " PROJ=5"

MailItem.Subject Property - Returns a String indicating Outlook item. Read/write.

Example

Item.Subject = "PROJ=5" & Item.Subject
  1. Append the Body with about 10 lines of text

Example

Dim olBody As String
olBody = "<HTML><BODY><P>Append the Body with about 10 lines of text</P>" & vbNewLine & vbNewLine & _
                     "<P>Append the Body with about 10 lines of text</P></HTML></BODY>" & vbNewLine

 olForward.HTMLBody = olBody & vbCrLf & olForward.HTMLBody
  1. Send / Forward Email

Example

'// 
Set olForward = Item.Forward
'// add Recipent
olForward.Recipients.Add "[email protected]"
'// Send or your use .Dispaly 
olForward.Send

Run a Script Rule

To use Rule Wizard, your macro has to have the expected parameter:

Example

Public Sub ItemForward(Item As Outlook.MailItem)
    
End Sub

Helpful article in MSDN Outlook 2010 VBA

Complete Code Test on Outlook 2010 VBA:

Please make sure your References are set to run action script (Tools > References)

Option Explicit
'// Run Action Script
 
Public Sub ItemForward(Item As Outlook.MailItem)
    Dim olApp As Outlook.Application
    Dim olForward As MailItem
    Dim olBody As String
    
    Set olApp = CreateObject("Outlook.Application")
    
    '// Append the Subject
    Item.Subject = "PROJ=5 " & Item.Subject
    Item.Save

     Set olForward = Item.Forward
    '// add Recipent
    olForward.Recipients.Add "[email protected]"
    
        
    olBody = "<HTML><BODY><P>Append the Body with about 10 lines of text</P>" & vbNewLine & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P>" & vbNewLine & _
                         "<P>Append the Body with about 10 lines of text</P></HTML></BODY>" & vbNewLine

             
    '// Forward Email
    olForward.HTMLBody = olBody & vbCrLf & olForward.HTMLBody
    '// Send or your use .Dispaly
    olForward.Send
    Set olApp = Nothing
    Set olForward = Nothing
End Sub