Click here to Skip to main content
15,881,092 members
Articles / Programming Languages / Visual Basic

Using User Defined Fields in Outlook

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Jul 2012CPOL5 min read 90.3K   7   6
Making use of the User Defined Fields to help better organize emails, and keep track of informaiton

Introduction

Anybody using Microsoft Outlook as their primary email handling and organizing application knows, that it a very easily customizable tool.

Less people know, that you can use the VBA to enhance your experience with Outlook, to tailor it to be the application you really need. 

And even fewer people know the true power behind User Defined Fields (UDF).

If you are working in an environment where you have to action the incoming request that are in the form of an email,  UDF can help you make your mailbox the request handling tool of your dreams. You can keep track which company sent it, how much time elapsed since the last time you took an action on the email, and even (this is my latest addition) keep track of a worklog, so you can track back, what happened to that email. 

Background 

Make no mistake, I'm not a real programmer. I'm more of what you call a Googrammer (Using Google to find the program code, and necessary knowledge on-demand). Still I managed to learn a lot of things in the past years. And this article is my collection and own development on the code being used by me and my colleagues every day.

Assumptions 

This article assumes that:

  • You have dealt with VBA or Visual Basic before
  • You have been using Microsoft Outlook in the past
  • You need more fields in you mailbox, that are either static or dynamic  

Basics

Customizing Outlook: Toolbars, Macros and Buttons  

In order to benefit from the macro that we are about to create, its better if you have these functions "at hand" rather then having to use Alt+F8 to run the macros.

After the toolbars are created, and the macros are added to the toolbar, you can always use them with a single click.

Please review the below links to get an understanding on how to create toolbars and add macros to them.

To add a macro to the toolbar you have just created, simply select the Macros option from Categories, and drag and drop the item on the toolbar.

Image Hosted by ImageShack.us

User Defined Fields 

User Defined Fields (or UDFs moving forward) are extra filed that you can create and then drag and drop to you mailbox, to store extra information you require. This can be Company, UserID, Cost etc...

You have quite the extensive fields built in,and you can add them from the filed chooser by right-clicking on the title bar of you mailbox, and selecting field chooser.

Image Hosted by ImageShack.us

It will give you a toolbox, from where you can just drag and drop items to your mailbox. 

If you scroll to the bottom in the dropdown menu at the top of the toolbox, you can get to  User Defined Fields. This is where you can create UDFs that you want to use in the future.

Image Hosted by ImageShack.us

 

You can click the New... button to create a new field and then set 

 In order to get in depth detail on User Defined Fields, please visit the below links:

Ok, I understand, whats the next step?  

After adding the fields, you can set outlook that these fields become editable on-click , but I found that rather uncomfortable, and even bad looking. So instead, we are going to use toolbars and macros to mange these fields. 

We will be using a rather simple macro to achieve the desired result. I will show you one simple example 

Using the code  

Ok. So now that you are familiar with the UDFs and the Optimization options, let's jump to the code right away.

In order for the code to work, you need to add a reference to the Microsoft Forms 2.0 Object Library.

You do that by opening the Visual Basic editor in Outlook (Alt+F11). Add a module to the Project using the project explorer. Doubleclick the module. Now go to Tools - References and either search for the Reference and check the checkbox next to it, or browse for the FM20.dll.

Image Hosted by ImageShack.us

If the above step is done, you can get to writing the code, you start by creating the sub:

VB.NET
Sub Customer(Optional ByVal Value As String, Optional ByVal IsCustomEntry As Boolean = False)

This will allow you to call the Sub with arguments, so you don't have to spam your module. You can set it to add a fix value toa field , like this:

VB.NET
Sub CustomerID_Customer1()

Call CustomerID("ABCD1234")

End Sub

Or, you can as the user for a specific input, which will trigger an inputbox:

VB.NET
Sub CustomerID_CustomerEntry()

Call CustomerID(, True)

End Sub 

Let's move onto the variables:

VB.NET
Dim i As Long 'Cycle Counter
Dim myCollection As Object 'Collection of the selected emails
Dim msg As Outlook.MailItem 'Defining the Mail Item
Dim objProperty As Outlook.UserProperty 'Defining the UDF Item
Dim UserDefinedFieldName As String 'Defining the Field name

I use UserDefinedFieldName so I can copy-paste the same function, and only change one variable's value to adapt to any UDF that I want to manage. 

VB.NET
Set myCollection = Outlook.Application.ActiveExplorer.Selection

This will set the selected items into one collection. The UDF will be applied to all Mail Itemsselected 

VB.NET
UserDefinedFieldName = "CustomerID"

This sets the UDF's name 

VB.NET
If IsCustomEntry = False Then ' No popup message
    If Not myCollection Is Nothing Then ' Check if selection is not empty
         For i = 1 To myCollection.Count ' Go through each item with each cycle
             Set msg = myCollection.Item(i) ' Set the current cycle's email
             Set objProperty = msg.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olTExt) ' Set UDF - Get UDF name from variable - Set UDF type
             objProperty.Value = Value ' Add value in arguments to UDF
             msg.Save ' Save MSG
         Next i
    End If 

If you have chosen to go with a fix value, the above cycle will run. If you chosen custom entry, the below cycle will run: 

VB.NET
ElseIf IsCustomEntry = True Then ' With popup message
   
    Value = InputBox("Please enter custom value", "Input custom field value") ' Get value from user
    
    If Not myCollection Is Nothing Then
         For i = 1 To myCollection.Count
             Set msg = myCollection.Item(i)
             Set objProperty = msg.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olTExt)
             objProperty.Value = Value
             msg.Save
         Next i
    End If
End If

And of course the necessary closing line Smile | <img src=   

VB.NET
End Sub     

Here is the whole code without interruption 

VB.NET
Sub CustomerID(Optional ByVal Value As String, Optional ByVal IsCustomEntry As Boolean = False)

Dim i As Long
Dim myCollection As Object
Dim msg As Outlook.MailItem
Dim oMail As Outlook.MailItem
Dim objProperty As Outlook.UserProperty
Dim UserDefinedFieldName As String

Set myCollection = Outlook.Application.ActiveExplorer.Selection

UserDefinedFieldName = "CustomerID"

If IsCustomEntry = False Then

    If Not myCollection Is Nothing Then
            'we selected multiple items
                For i = 1 To myCollection.Count
                    Set msg = myCollection.Item(i)
                    Set objProperty = msg.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olTExt)
                    objProperty.Value = Value
                    msg.Save
                Next i
   End If
    
ElseIf IsCustomEntry = True Then
    
    Value = InputBox("Please enter custom value", "Input custom field value")
    
     If Not myCollection Is Nothing Then
            'we selected multiple items
                For i = 1 To myCollection.Count
                    Set msg = myCollection.Item(i)
                    Set objProperty = msg.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olTExt)
                    objProperty.Value = Value
                    msg.Save
                Next i
    End If

End If
   
End Sub   

So after your done, you just call the function, add the arguments. You will have the macro available in the Customize menu, and you will be able to add it to the Toolbar you created fro this purpose

To be able to view what you have added, you have to create a UDF called "CustomerID", and than drag and drop the new UDF in the mailbox. 

Points of Interest

Please do not forget to add the reference... It gives an annoying error message, that will not help you to identify the problem.

You could probably add some error handling, in the macro, in case you try to edit a UDF of a non-Mail item, like a calendar invite or a task. I think a the simplest

VB.NET
On Error Resume Next

Is a sufficient solution to make the line jump to the next item, or you can also add a decision tree where you check whether myCollection.Item(i) is a MailItem or not. 

Final words 

If you use this cleverly, it can help you life a lot, like creating a dropdown menu in Outlook with the most common UDFs and their values, so you can manage you emails better and faster then before.

The UDFs can also grouping criteria. You can set a UDF to MSGSTATUS and add the values e.g.: Active, Pending, Non-Action, and then set Oultook to group emails according to the MSGSTATUS field.

You don't even need the UDF to be visible, Outlook will still group it using it's values! 

If you have any questions, please feel free to contact me!

History 

Article version 1.0 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks, perfect Pin
Rob Randell9-Feb-21 5:05
Rob Randell9-Feb-21 5:05 
GeneralMy vote of 5 Pin
Manjushri16-Mar-16 17:15
Manjushri16-Mar-16 17:15 
QuestionAn oldie, but a goodie. THANK YOU for this great article!! Pin
Manjushri16-Mar-16 17:14
Manjushri16-Mar-16 17:14 
QuestionNeed UDF in inbox to show filenames of attachments vs just paperclip icon Pin
Member 113499043-Jan-15 7:29
Member 113499043-Jan-15 7:29 
AnswerRe: Need UDF in inbox to show filenames of attachments vs just paperclip icon Pin
Kudredin16-Apr-15 0:34
Kudredin16-Apr-15 0:34 
GeneralRe: Need UDF in inbox to show filenames of attachments vs just paperclip icon Pin
Member 1134990420-Apr-15 17:15
Member 1134990420-Apr-15 17:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.