A visitor to our forums posted the following question in Batch adding text to email subject lines in Outlook at Outlook Forums:
We put file numbers in all communications with our clients. However, we frequently begin dialogues with potential clients before opening a file for them. Then, once they become a client and we open a file, we have to go back and manually change each un-numbered email subject line. It's normally pretty easy to come up with a search term that finds all the emails that need changing, but it's time-consuming to change each one manually. Is there a way to batch insert a string that looks like  into the subject lines of all the emails in a search result and/or folder?
Sure, you can do using VBA and a search folder by modifying the code sample at Copy: is prefixed to the Subject of a Meeting.
If there are just a few subject lines to edit, you can use in-cell editing to quickly paste the code into the subject.
Using VBA to change the subject line
Copy and paste the following code into the VB Editor. Create a search folder to find all messages where the subject needs tagged then run the macro while viewing the search folder results.
I tested this code in Outlook 2010 and it should work in all older versions. It works in Outlook 2013 and newer.
Sub AddFileNumber() Dim myolApp As Outlook.Application Dim aItem As Object Set myolApp = CreateObject("Outlook.Application") Set mail = myolApp.ActiveExplorer.CurrentFolder Dim iItemsUpdated As Integer Dim strTemp As String Dim strFilenum As String strFilenum = InputBox("Enter the file number") iItemsUpdated = 0 For Each aItem In mail.Items strTemp = "[" & strFilenum & "] " & aItem.Subject aItem.Subject = strTemp iItemsUpdated = iItemsUpdated + 1 aItem.Save Next aItem MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated" Set myolApp = Nothing End Sub
When you run the macro, a dialog box will come up asking for the file number (it accepts any text string). Type your file number or keywords in then press OK or Enter. When the macro is finished, a message will come up showing you how many items were changed.
Note: If you use this in Outlook 2010 and use Conversation view, the subject used for the conversation view won't change. Disable conversation view to see the updated subject lines on individual messages.
Also, if the search results include sent messages, those subject lines will be updated too.
Run a Script Rule to add Sequential Numbers to subject
This sample is a Run a Script rule and will add a number to each message as they arrive. It saves the last used number to the registry for use in the next session.
To use, create a rule, choosing the run a script action and select this macro as the script.
Sub SubjectNumbering(Item As Outlook.MailItem) Dim sAppName As String Dim sSection As String Dim sKey As String Dim lRegValue As Long Dim lFormValue As Long Dim iDefault As Integer sAppName = "Outlook" sSection = "Messages" sKey = "Current Number" ' The default starting number. iDefault = 101 ' adjust as needed ' Get stored registry value, if any. lRegValue = GetSetting(sAppName, sSection, sKey, iDefault) ' If the result is 0, set to default value. If lRegValue = 0 Then lRegValue = iDefault ' Increment and update invoice number. SaveSetting sAppName, sSection, sKey, lRegValue + 1 Item.Subject = CStr(lRegValue) & " " & item.subject Item.Save End Sub
Add a project number and the folder path to the subject
Jason wanted to add a project number and the path where the message is stored. To do this, I needed to add 2 1/2 lines to the original macro: Dim and Set the folder variable, then add the folder name variable to the subject.
This code adds the code and folder path to the subject of every message in the folder.
Sub AddFileNumberandFolderpath() Dim myolApp As Outlook.Application Dim aItem As Object Dim F As Outlook.MAPIFolder Set myolApp = CreateObject("Outlook.Application") Set mail = myolApp.ActiveExplorer.CurrentFolder Dim iItemsUpdated As Integer Dim strTemp As String Dim strFilenum As String strFilenum = InputBox("Enter the file number") iItemsUpdated = 0 For Each aItem In mail.Items Set F = aItem.Parent strTemp = "[" & strFilenum & "] " & aItem.Subject & " | " & F.FolderPath aItem.Subject = strTemp iItemsUpdated = iItemsUpdated + 1 aItem.Save Next aItem MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated" Set myolApp = Nothing End Sub
Change the subject of the selected or open message
Rachid wanted to know how to use this macro with only the selected item.
To run the macro on the selected message in the message list, replace the Set mail. line with the following line.
Set aItem = myolApp.ActiveExplorer.Selection.Item(1)
You'll also need to remove the code that loops through all messages (and the MsgBox line). (See code below.)
If you want the macro to work on selected or open messages, use the GetCurrentItem Function from work with open or selected items and the code below.
This code sample adds the ability to cancel the entry. To do this I changed Dim strFilenum As String to Dim strFilenum As Variant then added these the following lines. The first lines picks up the Cancel button, the second checks the value and if empty, exits.
If strFilenum = False Then Exit Sub
If strFilenum = "" Then Exit Sub
Sub AddFileNumber() Dim aItem As Object ' Don't forget the function Set aItem = GetCurrentItem() Dim iItemsUpdated As Integer Dim strTemp As String Dim strFilenum As Variant ' removed the loop so it only works with the selected item strFilenum = InputBox("Enter the file number") ' Empty value or cancel button If strFilenum = False Then Exit Sub If strFilenum = "" Then Exit Sub strTemp = "[" & strFilenum & "] " & aItem.Subject aItem.Subject = strTemp aItem.Save End Sub
Using In-cell Editing
If you don't need to change a large number of items, it may be faster to use in-cell editing.
Create a custom view or edit your existing view to enable in-cell editing then click in the subject field and paste the code.
How to use File Number Macros
This macro shows how to use the first macro and the run a script macro. After testing the macro, create a button on the ribbon or Quick Access Toolbar (QAT) to make it easy to use.