Ty Anderson

Create custom Outlook menus: VB.NET and C# for Outlook 2013-2003

It looks like Microsoft is going to go ahead and release Office 2013 sooner rather than later. I suppose you can argue they already have with the recent pre-ordering of Windows Surface (which comes with Office 2013 RT). Whether or not you consider Office 2013 RT to really be Office is up to you. It can be your opinion.

Whether you do or don’t, my point is the same. There is a new Office in town and he has a new deputy named Visual Studio 2012.

Life is fluid. Requirements evolve

Developers that want to use Add-in Express for Office to build with both of these “new guys” (i.e. Office 2013 and VS 2012) need to know we have you covered. Upgrading your solutions to Visual Studio 2012 is easy and Add-In Express fully supports each of them.

In this article, Part 4 of “Building a better Outlook add-in” series, I’ll show you how to add custom context menus and a custom main menu in the Outlook add-in (TyActions add-in). To make today truly exciting (even if you are un-caffeinated), we’ll upgrade the existing code to target the “new guys”.

So, for you bullet point lovers out there, we are going to do the following:

We won’t necessarily go in this order however. Let’s get to work.

Upgrading the solution to support Outlook 2013 and Visual Studio 2012

Upgrading the solutions is as easy as 1-2-3.

  1. Install the latest version of Add-in Express for Office and .NET (VB.NET, C#, C++.NET are supported)
  2. Open Visual Studio 2012
  3. Open the TyActions project in Visual Studio 2012 (you can download it at the bottom of part 3: How to work with Outlook objects and events)

That’s it. You’re ready to go and have full support of all version of Office 2013-2000. If you want to add debugging support to the TyActions project, complete step 4.

  1. Navigate to the TyActions properties tab and change Start external program setting to:
    C:\Program Files\Microsoft Office\Office15\OUTLOOK.EXE

Changing the Start external program parameter to debug the project in Outlook 2013

Caffeinated or not, I hope you’re excited and ready to tackle the tasks ahead.

Creating custom Outlook menus

We’ll add two custom context menus and a main menu to our Outlook plug-in. We need two right-click menus to support Outlook 2010 and 2013 as well as pre-2010 versions of Outlook. The custom main menu is superfluous in terms of the features we will add to them. But I don’t care. I’m including them because they are important to understand as you might need them one day.

Add the ribbon context menu to Outlook 2013 and 2010

Open the core part of your project, AddInModule, in design view, then add a new Ribbon Context Menu component (AdxRibbonContextMenu) using the AddInModule’s toolbar and complete the following steps.

  1. In the visual designer, select AdxRibbonContextMenu1.
  2. Find the ContextMenuNames property and click the ellipses button to open the Context Menu Names dialog box.
  3. In the first row, expand the drop down and select Outlook.Explorer.ContextMenuMailItem. Click the OK button.
  4. Change the Ribbons property to OutlookExplorer.
  5. In the Visual Designer, select the AdxReibbonContextMenu1 node within the hierarchy view (left-hand side). Add a new ribbon menu (AdxRibbonMenu control) by clicking its icon found in the visual designer’s menu. Change its properties to match these settings:
    1. Caption = Ty Actions
    2. ItemSize = Large
    3. Ribbons = OutlookExplorer
  6. With the AdxRibbonMenu1 object selected, add a new ribbon button (AdxRibbonButton control). The button should be a child object of AdxRibbonMenu1. If is not, go ahead and move it there. Now change the button’s properties as follows:
    1. Name = rbCreateTask
    2. ImageList = ImageList1
    3. Image = 0
  7. Add another AdxRibbonButton control and change these properties:
    1. Name = rbCreateAppointment
    2. ImageList = ImageList1
    3. Image = 1
  8. Add one last AdxRibbonButton control and change these properties:
    1. Name = rbCreateNote
    2. ImageList = ImageList1
    3. Image = 2

After you complete these steps you will have very nice looking Outlook ribbon right-click menu that resembles this one:

Outlook ribbon context menu in the in-place visual designer

This context menu works great for Outlook 2010 and 2013. But, one of our requirements is to support Outlook 2003-2010. What this means is we need to add a context menu that will work with versions prior to Outlook 2010.

Add custom context menus to Outlook 2007, 2003

Outlook 2010 is the first version to support the ribbon in the explorer window. This means we need to use traditional, non-ribbon or command-bar based, right click menus. So, let’s add one.
With the AddinModule open in design, add a CommandBar based Context Menu component (ADXContextMenu). If you’re living right, Visual Studio has already selected the context menu for you and is displaying its properties. Now, just edit the context menu’s properties to match these:

  • CommandBarName = Context Menu
  • SupportedApp = Outlook
  • Temporary = True

These properties tell the TyActions add-in which context menu should display our customizations and which Office application should display them. It is funny that the popup menu we target here has the name “Context Menu”. There are other right-click menus but “Context Menu” is the main one. Confused? Don’t be. It’ll all shake out… just follow my lead.

Just like with the ribbon context menu, we need to design the ADXContextMenu component by adding controls to it. In the AddinModule, select the ADXContextMenu1 object and open its visual designer if needed. Now, complete the following steps:

  1. Add a popup control (ADXCommandPopUp) and change these properties:
    1. BeforeID = 1
    2. Caption = Ty Actions
    3. OlExplorerItemTypes = Mail
    4. Temporary = True
  2. With the ADXCommandPopUp1 control selected (the one you added in Step #1), add a button (ADXCommandBarButton control). This button should display as a child of ADXCommandPopUp1. If it doesn’t, you can drag and drop it there using the visual designer’s hierarchy view. Change this button’s properties as follows:
    1. Name = cmbCreateTask
    2. Caption = Create Task
    3. ImageList = ImageList1
    4. Image = 0
    5. Style = adxMsoButtonCaption
    6. Temporary = True
  3. Add another ADXCommandBarButton, ensure it is under the TyActions ADXCommandPopUp1 control, and change these properties:
    1. Name = cmbCreateAppointment
    2. Caption = Create Task
    3. ImageList = ImageList1
    4. Image = 1
    5. Style = adxMsoButtonCaption
    6. Temporary = True
  4. Add one last ADXCommandBarButton, ensure it resides under the TyActions ADXCommandPopUp1 control, and change these properties:
    1. Name = cmbCreateNote
    2. Caption = Create Note
    3. ImageList = ImageList1
    4. Image = 2
    5. Style = adxMsoButtonCaption
    6. Temporary = True

And done. If you completed these steps, you should have another context menu in your visual designer that looks like this:

Command bar based Outlook context menu in the in-place visual designer

We need to add one more menu, Outlook main menu, and then we’ll write some code. Believe it or not, we are more than hallway done here.

Customizing the Outlook menu bar

The custom main menu we’ll create also targets pre-Outlook 2010 versions, i.e. Outlook 2007-2000. The main menu is the menu bar that resides above all traditional Office toolbars. The TyActions’s custom Outlook menu bar duplicates the functionality of the context menus as well as the custom toolbar added in a preview edition of this series. Because they are redundant (at least in terms of their underlying code), you can choose to add them or not. But for my sake, please read along so you know what they do in case you need to know about them some day.

With the AddinModule open in design view, add a special component to customize the main menu of Outlook Explorer (AdxOlExplorerMainMenu) to the design surface. To complete its design, follow these steps:

  1. Add a popup control (ADXCommandPopUp) and change these properties:
    1. Caption = Create Items from Email
    2. OlExplorerItemTypes = Mail
    3. Temporary = True
  2. With the ADXCommandPopUp2 control selected, add a custom button (ADXCommandBarButton control). This button should display as a child of ADXCommandPopUp2. Change this button’s properties as follows:
    1. Name = mmbCreateTask
    2. Caption = Create Task
    3. ImageList = ImageList1
    4. Image = 0
    5. Style = adxMsoButtonCaption
    6. Temporary = True
  3. Add another ADXCommandBarButton, ensure it is under the TyActions ADXCommandPopUp2 control, and change these properties:
    1. Name = mmbCreateAppointment
    2. Caption = Create Task
    3. ImageList = ImageList1
    4. Image = 1
    5. Style = adxMsoButtonCaption
    6. Temporary = True
  4. Add one last ADXCommandBarButton, ensure resides under the TyActions ADXCommandPopUp2 control, and change these properties:
    1. Name = mmbCreateNote
    2. Caption = Create Note
    3. ImageList = ImageList1
    4. Image = 2
    5. Style = adxMsoButtonCaption
    6. Temporary = True

After you complete these steps your custom Outlook menu bar should look similar to this:

Custom Outlook menu bar in the in-place visual designer

We are ready to write some code, this will not take long.

Coding the business logic of custom Outlook menus

The logic for our custom Outlook menus already exists. Well, that is if you have read the previous articles in this series and have completed their steps. I’ll assume this is the case and we’ll continue.

The custom Outlook right-click menus and the main menu all use the CreateLinkedItem function. The easiest way to add the code for each button’s click event is to add the following chunk of code:

VB.NET code example:

Private Sub cmbCreateTask_Click(sender As System.Object) _
	Handles cmbCreateTask.Click
 
	CreateLinkedItem(Outlook.OlItemType.olTaskItem)
End Sub
 
Private Sub cmbCreateAppointment_Click(sender As System.Object) _
	Handles cmbCreateAppointment.Click
 
	CreateLinkedItem(Outlook.OlItemType.olAppointmentItem)
End Sub
 
Private Sub cmbCreateNote_Click(sender As System.Object) _
	Handles cmbCreateNote.Click
 
	CreateLinkedItem(Outlook.OlItemType.olNoteItem)
End Sub
 
Private Sub rbCreateTask_OnClick(sender As System.Object, _
	control As AddinExpress.MSO.IRibbonControl, pressed As System.Boolean) _
	Handles rbCreateTask.OnClick
 
	CreateLinkedItem(Outlook.OlItemType.olTaskItem)
End Sub
 
Private Sub rbCreateAppointment_OnClick(sender As System.Object, _
	control As AddinExpress.MSO.IRibbonControl, pressed As System.Boolean) _
	Handles rbCreateAppointment.OnClick
 
	CreateLinkedItem(Outlook.OlItemType.olAppointmentItem)
End Sub
 
Private Sub rbCreateNote_OnClick(sender As System.Object, _
	control As AddinExpress.MSO.IRibbonControl, pressed As System.Boolean) _
	Handles rbCreateNote.OnClick
 
	CreateLinkedItem(Outlook.OlItemType.olNoteItem)
End Sub
 
Private Sub mmbCreateTask_Click(sender As System.Object) _
	Handles mmbCreateTask.Click
 
	CreateLinkedItem(Outlook.OlItemType.olTaskItem)
End Sub
 
Private Sub mmbCreateAppointment_Click(sender As System.Object) _
	Handles mmbCreateAppointment.Click
 
	CreateLinkedItem(Outlook.OlItemType.olAppointmentItem)
End Sub
 
Private Sub mmbCreateNote_Click(sender As System.Object) _
	Handles mmbCreateNote.Click
 
	CreateLinkedItem(Outlook.OlItemType.olNoteItem)
End Sub

As you no doubt noticed, there is nothing complicated going on here. Each button event calls CreatedLinkedItem and specifies the type of Outlook item to create. The trick is to make sure we match the Outlook type with the correct buttons.

Preventing pre-Outlook 2010 UI controls from displaying in Outlook 2010 and 2013

Just like in Part 3, we need to prevent pre-Outlook 2010 user interface elements from displaying in Outlook 2010 and Outlook 2013. This task is easily accomplished with two lines of code. I have included the full AddinInitialize event here for easier copying and pasting. For your convenience, I have highlighted the two new lines.

Private Sub AddinModule_AddinInitialize(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) _
	Handles MyBase.AddinInitialize
 
    'Grab Task folders so we can monitor changes
    Dim ns As Outlook.NameSpace = OutlookApp.Session
    Dim tasksFolder As Outlook.Folder = ns.GetDefaultFolder( _
		Outlook.OlDefaultFolders.olFolderTasks)
    fldrs = New OutlookFoldersEventsClass1(Me)
    fldrs.ConnectTo(tasksFolder, True)
    Marshal.ReleaseComObject(ns)
    '''
 
    If Me.HostMajorVersion >= 14 Then
      AdxOlExplorerCommandBar1.UseForRibbon = False
 
      ''NEW FOR PART 4!!!!
      AdxOlExplorerMainMenu1.UseForRibbon = False
      AdxContextMenu1.UseForRibbon = False
    Else
      LoadTaskFoldersToComboBox()
      cbcbTaskFolders.Text = "Tasks"
    End If
End Sub

Custom menus is Outlook 2013, 2010 – 2003

Let’s see some action from our Outlook add-in. Go ahead and build the project and register it on your system. Both of these commands reside in Visual Studio’s Build menu (or should I say BUILD menu?).

Now open Outlook 2013 and select a mail item. The custom ribbon should be visible but who cares?! We are here to see the custom context menus. Right click the selected mail item and I hope you see a right-click menu similar to the one below.

The custom context menu in Outlook 2013:
The custom context menu in Outlook 2013

If you have a system with a pre-Outlook 2010 version of Outlook installed, you can run the project there and test it out. That is, if you have Visual Studio and ADX installed on it. If not, you can create a setup project and install the TyActions add-in. Or you can take a look at this screenshot and have confidence it works in Outlook 2003 as well.

The custom context menu in Outlook 2010:
The custom context menu in Outlook 2010

The custom context menu in Outlook 2003:
The custom context menu in Outlook 2003

Available downloads:

This sample Outlook plug-in was developed using using Add-in Express for Office and .net:
VB.NET – Ty Actions Outlook Add-in

How to write better Outlook add-ins

6 Comments

  • Santhosh says:

    Hi, I have looked to the above post it was useful. But one thing I like to know is it possible to create the context menu dynamically at runtime. If I could purchase your Add-in tool. Let me know your feedback on it,

  • Ty Anderson says:

    Hello Santhosh,

    Yes, this is entirely possible. Can you provide more information regarding your exact scenario?

    Ty

  • Santhosh says:

    Could you send your Email-id so that I will send you the exact scenario in the document.

  • Ty Anderson says:

    Hi Santhosh

    What you want to do is use the ADXRibbonMenu’s OnCreate event to build the menu.
    As I understand it, you have a form that you will use to define some URLs.
    Each URL you create with the form will reside in the add-in settings.

    You want to you use the OnCreate to read the settings and add buttons for each URL the user has defined.

    Here is a sample OnCreate event that you can add to this post’s sample code:

    Private Sub AdxRibbonMenu1_OnCreate(sender As Object, e As ADXRibbonCreateMenuEventArgs) Handles AdxRibbonMenu1.OnCreate

    ' clear dynamic menu
    e.Clear(True)

    ' add buttons

    ' Create Task
    Dim ctl As ADXRibbonButton = New ADXRibbonButton()
    ctl.Caption = "Create Task"
    ctl.Id = "my_id_create_task"
    ctl.Image = 0
    ctl.ImageList = Me.ImageList1
    ctl.ImageTransparentColor = System.Drawing.Color.Transparent
    ctl.Ribbons = ADXRibbons.msrOutlookExplorer
    AddHandler ctl.OnClick, AddressOf rbCreateTask_OnClick
    e.AddControl(ctl)

    ' Create Appointment
    ctl = New ADXRibbonButton()
    ctl.Caption = "Create Appointment"
    ctl.Id = "my_id_create_appointment"
    ctl.Image = 2
    ctl.ImageList = Me.ImageList1
    ctl.ImageTransparentColor = System.Drawing.Color.Transparent
    ctl.Ribbons = ADXRibbons.msrOutlookExplorer
    AddHandler ctl.OnClick, AddressOf rbCreateAppointment_OnClick
    e.AddControl(ctl)

    ' Create Note
    ctl = New ADXRibbonButton()
    ctl.Caption = "Create Note"
    ctl.Id = "my_id_create_note"
    ctl.Image = 1
    ctl.ImageList = Me.ImageList1
    ctl.ImageTransparentColor = System.Drawing.Color.Transparent
    ctl.Ribbons = ADXRibbons.msrOutlookExplorer
    AddHandler ctl.OnClick, AddressOf rbCreateNote_OnClick
    e.AddControl(ctl)

    ' One more new button
    ctl = New ADXRibbonButton()
    ctl.Id = "my_dynamic_button_id"
    ctl.Caption = "Dynamic Button (" & DateTime.Now.Ticks & ")"
    ctl.Ribbons = ADXRibbons.msrOutlookExplorer
    e.AddControl(ctl)
    End Sub

    You will need to change it to use your add-in’s settings file.

    Ty

    BTW – Dmitry deserves an assist on this one.

  • yama says:

    Dear Ty Anderson.

    thank you for your post.
    but i want to that when selected multi email and click right mouse, it will display Try Action in the content menu.

    Please help me. thank you!.

  • Andrei Smolin (Add-in Express Team) says:

    Hello yama,

    Install the add-in we describe at https://www.add-in-express.com/creating-addins-blog/creating-office-context-menu-addin/ to find the name of the context menu which is open when you right-click selected objects. Then modify the add-in above to use that menu name by the ADXRibboncontextMenu component; see step #3 in section Add the ribbon context menu to Outlook 2013 and 2010 above.

Post a comment

Have any questions? Ask us right now!