Customizing Menus and Toolbars in Outlook

One of the main points of entry to code in Microsoft Office 2003 applications is through their menus and toolbars. Using the code you created in the preceding section to add a task, you can add additional code to call this code from a menu item that you create. In this example, you will delete the toolbar if it already exists and then re-create it.

We take this approach instead of setting a reference to the existing toolbar (if found) because we don't know what state the toolbar was in when Outlook last closed, and whether the toolbar buttons were created correctly. Rather than check for the toolbar's existence and the functionality of each button, it is easier to delete and re-create the toolbar. This happens only once during application startup.

1. Create an Outlook add-in project in VSTO.

2. In Solution Explorer, right-click ThisApplication and click View Code.

3. Add the code in Listing 10.11 to the Startup event handler of ThisApplication. Note that the variables are created outside the Startup event handler, at the class level.

Listing 10.11. Checking for existing menus and re-creating them

Private menu As Office.CommandBar

Private menuPopup As Office.CommandBarPopup WithEvents CreateTask As Office.CommandBarButton

Private Sub ThisApplication_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

' Check whether menu exists and, if it does, remove it. Dim checkMenu As Office.CommandBarPopup = _

Me.ActiveExplorer().CommandBars.ActiveMenuBar. _ FindControl(Office.MsoControlType.msoControlPopup, _ System.Type.Missing, "Tasks", True, True)

If checkMenu IsNot Nothing Then checkMenu.Delete(True) End If

' Call code to create new menu. AddMenuBar()

4. Add the code for the AddMenuBar method, as shown in Listing 10.12. This code adds a menu named Tasks and a menu item that displays the caption "Create Task." The FaceID 837 is the icon for Outlook tasks.

Listing 10.12. Creating a new menu

Private Sub AddMenuBar() Try menu = Me.ActiveExplorer().CommandBars.ActiveMenuBar menuPopup = menu.Controls.Add( _

Office.MsoControlType.msoControlPopup, _ Temporary:=True) If menuPopup IsNot Nothing Then menuPopup.Caption = "Tas&ks" menuPopup.Tag = "Tasks" CreateTask = menuPopup.Controls.Add( _

Office.MsoControlType.msoControlButton, _ Before:=1, Temporary:=True)

With CreateTask

.Style = Office.MsoButtonStyle _

.msoButtonIconAndCaption .Caption = "Create Task" .FaceId = 837 .Tag = "CreateTask" End With menuPopup.Visible = True End If Catch Ex As Exception MsgBox(Ex.Message) End Try End Sub

5. Add the code in Listing 10.13 to the Click event handler of the CreateTask menu item.

Listing 10.13. Creating a task from the Click event of the menu item

Private Sub CreateTask_Click(ByVal Ctrl As _ Microsoft.Office.Core.CommandBarButton, _ ByRef CancelDefault As Boolean) Handles CreateTask.Click Dim NewTask As Outlook.TaskItem = _ Me.CreateItem(Outlook.OlItemType.olTaskItem)

With NewTask

.Subject = "My new task" .DueDate = System.DateTime.Now

.Body = "This is my new Task which is due right now!" .Status = Outlook.OlTaskStatus.olTaskInProgress .Display() End With End Sub

6. Press F5 to run the code. When Outlook opens, a new Tasks menu appears, as shown in Figure 10.13. When you click the menu item Create Task, the new task item is created and displayed.

Figure 10.13. Menu and menu item

This code is very similar to the code you would use to create a menu and menu item in Word and Excel, except that you use the Explorer object. Outlook also enables you to add menus and toolbars to individual Inspectors, such as an e-mail item, a task item, or a meeting request. It can be particularly challenging to ensure that the code behind your buttons works correctly when you open multiple Inspectors. This is because the menu is added only to the first Inspector that is opened. Appendix B explains how to resolve this issue.

0 0

Post a comment