Last reviewed on May 7, 2014   —  20 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

Comments

  1. Rachid says

    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

    • Diane Poremsky says

      You need to use the Function at work with open or selected items

      Sub AddFileNumber()
      Dim aItem As Object

      ' use this instead of the other two sets - don't forget the function
      Set aItem = GetCurrentItem()

      Dim iItemsUpdated As Integer
      Dim strTemp As String
      Dim strFilenum As String

      ' remove the loop so it only works with the first selected items
      strFilenum = InputBox("Enter the file number")
      strTemp = "[" & strFilenum & "] " & aItem.Subject
      aItem.Subject = strTemp
      aItem.Save

      End Sub

  2. Rachid says

    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?

    • Diane Poremsky says

      Yes, you need to add a Cancel routine. I figured they would only run the macro if they wanted to add a value.

      Cheap method: if no value is entered in the box, exit (added after strfilenum line)
      If strFilenum = "" Then Exit Sub

      To get the cancel button, change one line and add another:
      'change this from String to variant
      Dim strFilenum As Variant
      'add this after inputbox line:
      If strFilenum = False Then Exit Sub

      You can use both lines to trap empty values and the cancel button.

  3. pqavina says

    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.

    • Diane Poremsky says

      do you want to apply a category and have the macro pick up the change? If the folders are in one path, like under the inbox, it's fairly easier to match the category to the folder.

      This moves the selected item to a folder named for the category. I'm not sure of the ItemChange event picks up category change. Oh and it only works with one category ( it sees a string of categories as 1 string, not several categories)

      Set aItem = myolApp.ActiveExplorer.Selection.item(1)
      strCat = aItem.Categories
      Set destFolder = Session.GetDefaultFolder(olFolderInbox).Folders(strCat)
      aItem.Subject = Format(Now, "YYMMDD") & " | " & aItem.Categories & " | " & aItem.Sender & " | " & aItem.To & " | " & aItem.Subject
      aItem.Save
      aItem.Move destFolder
      Set myolApp = Nothing
      End Sub

  4. Stephen says

    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

    • Diane Poremsky says

      i have a sample here that moves - Run a script - move messages - actually, it uses poor form as it has the path in the move instead of a variable.

      Try Set destFolder = Session.GetDefaultFolder(olFolderInbox).Folders("subfolder-name")

      That is a subfolder of the inbox. For a folder at the same level, use parent.folders:
      Session.GetDefaultFolder(olFolderInbox).Parent.Folders("No Subject").Items

      My only other recommendation is to set it up as a run a script rule so its all automated.

    • Diane Poremsky says

      The code contains a For... Each block with works on all messages in the current folder. To apply it to selected items, replace For Each aItem In Mail.Items line with this:

      Dim Selection As Selection
      Set Selection = Application.ActiveExplorer.Selection
      For Each aItem In Selection

  5. Matt Walker says

    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

    • Diane Poremsky says

      Is this for incoming email or outgoing? You can parse text in the subject and search contacts to cross reference. If the number format is always the same it will be a little easier - you can count numbers, otherwise, you may need to use regex. I'm not sure if i have any search code samples.

    • Louis says

      I can't seem to get his code to work any help would be appreciated

      Sub AddFileNumber()
      Dim aItem As Object

      ' Don't forget the function
      Dim objApp As Outlook.Application

      Set objApp = Application
      On Error Resume Next
      Select Case TypeName(objApp.ActiveWindow)
      Case "Explorer"
      Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
      End Select

      Set objApp = Nothing

      Set aItem = myolApp.ActiveExplorer.Selection.Item(1)

      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

  6. Matt Walker says

    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

  7. Ellrick says

    Hi all, I've tried to modify first code from the top of this page but without luck, I'm trying to add sequence numbers in brackets to each subject in active folder, for example: message number 1 [1]Subject
    message number 2 [2]Subject
    message number 3 [3]Subject

    but every time I get new message and I run script I want previous number to be gone
    so it won't be like:

    message number 1 [1][1]Subject
    message number 2 [2][2]Subject
    message number 3 [3][3]Subject

    etc.

    is it possible?

    Appreciate any help.

    Thanks

    • Diane PoremskyDiane Poremsky says

      You need to either remove the number and add it back or track the last used # and skip messages with numbers already. The easiest fix is to check for the changes already:
      If Left(aItem.Subject, 1) <> "[" Then
      strTemp = "[" & strFilenum & "] " & aItem.Subject

  8. kanchan says

    how to add a keyword and datetime with the end of selected mails subject line and move selected mail from inbox to another folder in outlook?

Leave a Reply