An Outlook user wanted to know if she could assign a number or code to messages. She was tasked with copying messages from several mailboxes into one folder and wanted to number each message and code the messages so she could quickly see which mailbox the message came from.
This macro adds the "received by name" to a custom field; in most cases, this will be the mailbox name that received the message. The numbering starts at zero and remembers the last number used while Outlook is open. If you need to remember the last number used between Outlook sessions, you need to write the number to a file or registry (see second macro).
To use, add the macro to a module in the VB Editor, select some messages and run it. It works on selected messages in any folder.
Add an Index code to selected messages
This macro creates a custom field on each selected messages and adds a code to it, in the format of "mailboxname number". The field is sortable, although the numbers will sort as text, not numbers.
Dim i As Long Public Sub CreateIndexCode() Dim currentExplorer As Explorer Dim Selection As Selection Dim obj As Object Dim objProp As Outlook.UserProperty Dim strDomain As String, strAcct As String Dim propertyAccessor As Outlook.propertyAccessor Set currentExplorer = Application.ActiveExplorer Set Selection = currentExplorer.Selection On Error Resume Next For Each obj In Selection ' Add mailbox name Set propertyAccessor = obj.propertyAccessor strAcct = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0040001E") strDomain = strAcct & " " & i Set objProp = obj.UserProperties.Add("Index", olText, True) objProp.Value = strDomain obj.Save i = i + 1 Err.Clear Next Set currentExplorer = Nothing Set obj = Nothing Set Selection = Nothing End Sub
Write the last used value to the registry sample
This version of the macro writes the index number to the registry. This allows you to close and restart Outlook and pickup the numbering where you left off.
The field in this version is a number field. If you sort on it, it will sort in numerical order.
Public Sub SetIndex() Dim currentExplorer As Explorer Dim Selection As Selection Dim obj As Object Dim objProp As Outlook.UserProperty Dim strIndex As String Dim propertyAccessor As Outlook.propertyAccessor Dim sAppName As String Dim sSection As String Dim sKey As String Dim lRegValue As Long Dim lFormValue As Long Dim iDefault As Integer ' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Outlook\Index sAppName = "Outlook" sSection = "Index" sKey = "Last Index 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 Set currentExplorer = Application.ActiveExplorer Set Selection = currentExplorer.Selection On Error Resume Next For Each obj In Selection strIndex = lRegValue ' I'm using a number field - the field will sort correctly Set objProp = obj.UserProperties.Add("Index", olInteger, True) objProp.Value = strIndex obj.Save lRegValue = lRegValue + 1 Err.Clear Next ' Increment and update invoice number at the end SaveSetting sAppName, sSection, sKey, lRegValue Set currentExplorer = Nothing Set obj = Nothing Set Selection = Nothing End Sub
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
Hi Diane,
Thank you so much for your support to the world!
I need your support in backing up emails.
Based on your posts, i had already wrote the macro to backup an email with date, time, from, To and subject. It works well! Instead of recording time i need to add serial number from 1 to x for the emails with same subject and same sender and receiver. This method is easy to sort the emails in the explorer folder based on sender and receiver. Can you please help ?
Adding a serial number is easy, doing it by message is harder unless are doing it by conversations, one at a time. Otherwise you need to check each subject and track the last used serial, which can be difficult if you have a lot of different conversations.