Add a file number or keyword to the subject line of messages

Last reviewed on May 7, 2014   —  16 comments

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 [12345] into the subject lines of all the emails in a search result and/or folder?

message sample showing edited subjectsSure, 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.

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.

Enter the file number into the input box.

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.

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.

The steps are at Change the Subject of an Incoming Message and for Outlook 2010, see Editing the Subject line in Outlook 2010

Written by

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.

Please post long or more complicated questions at Outlookforums.

16 responses to “Add a file number or keyword to the subject line of messages”

  1. Rachid

    Great code, one question tho; what changes do I need to make in order to make this work on the selected item only?

    Giving users a button that prompts them for a text to insert is so much more appealing then having them use the in-line editor.

    Thanks

  2. Rachid

    Really appreciate the updated code, works like a charm.

    I tried fixing the following myself but I need to dive deeper into VBA in order to find out how;
    when clicking Cancel in the dialog box, it will still add the value defined in strTemp (i.e. the two brackets).

    Do you have a fix for this?

  3. pqavina

    Hi guys,

    I would like to create a script so that I could simply categorise emails and they will be moved to a folder with that category reference label but also then rename the subject so that it added "YYMMDD" "category" "sender" to "receiver" followed by the existing subject and "(with attachments)" if any exist.

  4. Stephen

    Hello,

    I have a addin that does not count emails with blank subject lines which I actually recieve quiet a lot of. So I would like to add a subject to any email that comes into my inbox with a blank subject and then have it move to another folder so it can be moved back and be picked up. I have managed to botch together the code below that changes the subject but have hit a wall everytime I try and add in the move function? I literally want these emails to be renamed, moved and then there will be a rule to move it back to the inbox so it can be counted.

    Please could someone point me in the right direction?
    Sub RenameBlankSubject()
    Dim myOlApp As Outlook.Application
    Dim aItem As Object
    Set myOlApp = CreateObject("Outlook.Application")
    Set mail = myOlApp.ActiveExplorer.CurrentFolder
    ' I have tried adding set destfolder here
    ' Set destFolder =
    Dim iItemsUpdated As Integer
    Dim strTemp As String
    Dim strFilenum As String
    For Each aItem In mail.Items
    If Trim(aItem.Subject) = "" Then
    aItem.Subject = "Your Enquiry"
    End If
    aItem.Save
    ' And a 'aItem.Move destFolder' here
    ' aItem.Move destFolder
    Next aItem
    Set myOlApp = Nothing
    End Sub

  5. david

    Hi Diane,

    Great concept but how can I run through multiple items instead of just one item at a time?

  6. david

    That works like a charm! Thank you very much Diane!

  7. Matt Walker

    Hi, is there a way to modify this to read a phone or fax number in the subject line and replace it with a contact name if they exist in my contact list?
    Thanks,
    Matt

  8. Matt Walker

    Hi Diane,

    Thanks for the reply. It is for incoming email. They are all faxes that have been converted to email format and then sent to us and the subject format is always the same.
    Thanks,
    Matt

Leave a Reply

If the Post Coment button disappears, press your Tab key.