• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Add a keyword to the subject of all messages sent

Slipstick Systems

› Developer › Add a keyword to the subject of all messages sent

Last reviewed on September 21, 2022     9 Comments

Lili wanted to know how to create a macro that will add "Privacy" to the subject of every message she sends, if privacy is not already in the subject.

This is easy using an ItemSend event. My code looks for the word anywhere in the subject, to account for RE: and FW:. You could use Left(Item.Subject, 6) or Right(Item.Subject, 6) = "Privacy" if you want to look for the word at the beginning or end of the subject.

Public WithEvents myOlApp As Outlook.Application

Private Sub Application_Startup()
    Initialize_Handler
End Sub

Public Sub Initialize_handler()
    Set myOlApp = CreateObject("Outlook.Application")
End Sub

Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)

 If InStr(1, Item.Subject, "Privacy", vbTextCompare) = False Then 
    Item.Subject = "Privacy " & Item.Subject
 End If
End Sub 

Add the keyword on send

This version of the macro adds the keyword when you send the message.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
 Dim objRecip As Recipient
 Dim strMsg As String
 Dim res As Integer

 On Error Resume Next

 Dim strTemp As String
 Dim strFilenum As Variant

 strFilenum = InputBox("Enter the file number")

 If strFilenum = "" Then
       Cancel = True
       MsgBox "The file number was blank or you clicked Cancel." _
       & vbCrLf & "click Send and select No if you don't want to BCC & add a file number."
       Exit Sub
     Else
       strTemp = "[" & strFilenum & "] " & Item.Subject
       Item.Subject = strTemp
       Item.Save
 End If

End Sub

 

BCC and add a code to the subject

In this version of the macro, the user is asked if he wants to BCC the message. If yes, then he needs to add a code to the subject. If he leaves the code blank or cancels it, the message is not sent.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
 Dim objRecip As Recipient
 Dim strMsg As String
 Dim res As Integer
 Dim strBcc As String

 On Error Resume Next

 strBcc = "me@domain.com"
 res = MsgBox("BCC this message?", vbYesNo + vbDefaultButton1, _
 "BCC Message")

 If res = vbNo Then
   Cancel = False
 Else

 Dim strTemp As String
 Dim strFilenum As Variant

 strFilenum = InputBox("Enter the file number")

 If strFilenum = "" Then
       Cancel = True
       MsgBox "The file number was blank or you clicked Cancel." _
       & vbCrLf & "click Send and select No if you don't want to BCC & add a file number."
       Exit Sub
     Else
       strTemp = "[" & strFilenum & "] " & Item.Subject
       Item.Subject = strTemp
       Item.Save
 End If

 Set objRecip = Item.Recipients.Add(strBcc)
     objRecip.Type = olBCC

 If Not objRecip.Resolve Then
     strMsg = "Could not resolve the Bcc recipient. " & _
          "Please check the BCC Script configuration. " & _
          "Do you want still to send the message?"
     res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
           "Could not resolve BCC")

 If res = vbNo Then
    Cancel = True
 End If
 
End If
End If

End Sub

Customize the macro

To use this macro to do other things when you send a message, remove or replace the contents of myOlApp_ItemSend macro. Remove everything from Dim... to End If and inset your code.

For example, to display the Category picker dialog when you send a message, use

Item.ShowCategoriesDialog

ItemSend macro

How to use the macro

To use this code, open the VB editor using Alt+F11. Expand Project1 to locate ThisOutlookSession and paste the code at the top of ThisOutlookSession.

Click in the Application_Startup macro and press F5 or the Run button to kick start the macro without restarting Outlook.

Oh, and don't forget to check your macro security settings in Trust Center, Macro Security. (Tools, Macros, Security in older versions of Outlook.)

Add a keyword to the subject of all messages sent was last modified: September 21st, 2022 by Diane Poremsky

Related Posts:

  • Add a file number or keyword to the subject line of messages
  • A macro that checks the number of recipients on an outgoing Outlook me
    Check messages you send for number of recipients
  • Use VBA to check Outlook's outgoing messages for attachments and cance
    Do you want to send an attachment?
  • Automatically BCC All Messages

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
9 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Lilia (@guest_172001)
October 11, 2012 11:13 am
#172001

I see where you put the code. I movef it to thay same location and i didnt get an error that time but it didnt run the macro automatically when outlook started either

0
0
Reply
Lilia (@guest_171999)
October 11, 2012 9:57 am
#171999

I tried and when i hit run i got an error compile error invalid function in sub or function

0
0
Reply
Diane Poremsky (@guest_172000)
Reply to  Lilia
October 11, 2012 10:23 am
#172000

Did you put it in ThisOutlookSession? Was any of the code in red?

0
0
Reply
Diane Poremsky (@guest_171997)
October 11, 2012 9:38 am
#171997

Sorry - Ijust realized that there is no app startup - add this to it:

Private Sub Application_Startup()
Initialize_Handler
End Sub

0
0
Reply
Lilia (@guest_171995)
October 11, 2012 8:55 am
#171995

Another question. How do i make it to autorun every time outlook starts. Right now i have to manually run it when i start outlook. My current setting are set to default warnings for all macros and i am not allowed to change because of administrative settings by my company

0
0
Reply
Diane Poremsky (@guest_171996)
Reply to  Lilia
October 11, 2012 9:29 am
#171996

It should run automatically when you send a message. If it's not, click in the Initialize_handler macro and click Run - but you should only need to do that while testing. When you start Outlook, it should kick in on its own.

0
0
Reply
lilivr112 (@guest_171973)
October 9, 2012 9:59 am
#171973

Thank you for your help.

0
0
Reply
lilivr112 (@guest_171967)
October 9, 2012 4:47 am
#171967

Thank you for the help. I do need "Privacy" to be writen at the end of the subject but i must not be putting Right(Item.Subject, 6) = in the right place because it erros out when i wrote Right(Item.Subject, 6) = "Privacy " & Item.Subject. i want to check the whole subject if the word Privacy is in there and if not to put it at the end of the subject. what is the correct script? Another questions, if the email already had Privacy in the subject but it was typed with capital letters, would the macro recognize that PRIVACY is in the subject or now? Thank you

0
0
Reply
Diane Poremsky (@guest_171972)
Reply to  lilivr112
October 9, 2012 8:08 am
#171972

You want to check the entire subject:

If InStr(1, Item.Subject, "Privacy", vbTextCompare) = False Then

that will find the word anywhere in the subject, beginning with the first letter, and is case insensitive. (It correctly identified pRiVacy in a subject)

This will add it to the end of the subject:

Item.Subject = Item.Subject & " Privacy"

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 19

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment