Converting a Recorded VBA Macro to Visual Basic in VSTO

In this section, you will record a simple macro in VBA, but instead of associating the macro with a toolbar button using the Customize dialog box (as shown in Figure 6.1), you'll convert the code to Visual Basic 2005. You can then add the code to the Click event handler of a toolbar button that you'll create programmatically using VSTO.

Figure 6.1. Using the Customize dialog box to associate VBA macros with menu and toolbar items

Recording a Macro in Word

1. Open a Word 2003 document outside Visual Studio, and type some text in the first paragraph of the document.

2. Select some text within the first paragraph, and then click Tools, point to Macros, and click Record New Macro.

3. In the Record Macro dialog box, type Boldltalic in the Macro Name box, and then click OK.

4. On the Formatting toolbar, click the Bold button, and then click the Italic button.

5. On the Stop Recording toolbar, click Stop Recording.

6. Open the VBE by pressing ALT+F11.

Listing 6.2 shows the VBA code you should see in the VBE. Notice that the macro recorder generated the enumeration wdToggle to assign bold and italic formatting to the selection. This means that if the selection is not already bold and italic when the macro is run, it will apply bold and italic; otherwise, it will remove the bold and italic formatting.

Listing 6.2. Recorded VBA macro Sub BoldItalic()

' Sample Macro

' Macro recorded by Kathleen McGrath

Selection.Font.Bold = wdToggle Selection.Font.Italic = wdToggle End Sub

If you open the Customize dialog box, you will see a macro named Project.NewMacro.BoldItalic listed as a command in the Macros categories. At this point you could drag the macro from the dialog box to an area on the toolbar.

We cannot access this dialog box inside Visual Studio while Word is open (and even if we could, the dialog box gives us no way to access VSTO code associated with a Word document or template). This means that you must programmatically add a button to a toolbar and then write code in the button's Click event handler so that it will toggle the formatting. You can access the button's Click event when you create the variable WithEvents, as shown in Listing 6.3.

1. Create a Word document solution using VSTO.

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

3. Add the code in Listing 6.3 to the ThisDocument class, replacing the existing Startup event handler.

Listing 6.3. Creating a toolbar button to toggle bold and italic formatting

Dim commandBar As Office.CommandBar

WithEvents formatButton As Office.CommandBarButton

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

Me.Application.CustomizationContext = Me.InnerObject commandBar = Me.CommandBars("Standard") formatButton = CType(commandBar.Controls.Add(1), _ Office.CommandBarButton)

With formatButton

.Caption = "Bold and Italic" .Tag = "FormatButton" .FaceId = 34 End With

End Sub

Private Sub formatButton_Click(ByVal Ctrl As _

Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault _ As Boolean) Handles formatButton.Click

Dim Selection As Word.Selection = Me.Application.Selection

Selection.Font.Bold = Word.WdConstants.wdToggle Selection.Font.Italic = Word.WdConstants.wdToggle

End Sub

The variables for the CommandBar and CommandBarButton objects are created outside the Startup event handler. This is because we need to access these variables from the method that handles the button's Click event. If we were to declare the variables in the Startup event handler, the variables would go out of scope after the code in the procedure has been run. After the variable is out of scope, it can be garbage col lected, and that would prevent the button's Click event handler from firing. We also set the CustomizationContext to ThisDocument (represented by the keyword Me) so that the customization is saved only in the document associated with the code, rather than in the template (the default).

Notice that the code that we added to the button's Click event handler has been revised from the code in the recorded macro. Because the Selection object is available only through the Application object, you need to create a variable for the selection rather than access it directly as you can in VBA. The remainder of the code is identical to what was recorded in VBA except that the wdToggle enumeration must be fully qualified in Visual Basic 2005. When you run this code, a new button is added to the Standard toolbar. Each time the button is clicked, the bold and italic formatting is either applied to or removed from the selected text.

Instead of providing a built-in icon for the button by supplying a FaceID, you can create a custom image and add it to the control. You will learn how to do this in Chapter 8.

0 0

Post a comment