Creating Inspector Command Bars

In VBA, when you add a button to a CommandBar, you associate a macro with the OnAction property of the button. Using VSTO, you must program-matically create a CommandBarButton and add it to the CommandBar.

Outlook presents some challenges when you're creating CommandBars and CommandBarButtons. For every CommandBarButton that you create, you must hold a reference to it in order for the Click event handler to function properly. In this example, we show you how to create an Inspector CommandBar and add a button to it.

1. Create an Outlook add-in using VSTO.

2. Add the code in Listing B.1 to the Startup event handler of ThisApplication, and add the variables with events at the class level.

Listing B.1. Creating a variable for an Outlook Inspector

' Create an inspector and CommandBar button object with events. Dim WithEvents MyInspector As Outlook.Inspectors Dim WithEvents MyButton As Office.CommandBarButton Dim MyCommandBar As Office.CommandBar

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

MyInspector = Me.Inspectors

End Sub

3. Add the code in Listing B.2 to the Shutdown event handler of ThisApplication.

Listing B.2. Releasing objects in the Shutdown event handler

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

' Release the objects. MyInspector = Nothing MyButton = Nothing

End Sub

0 0

Post a comment