Programming in Word

Word is often used by solution developers for its formatting capabilities. It is easy to create documents such as reports or letters using Word. VSTO provides a toolset in Visual Studio that enables you to create rapid application development (RAD) solutions by displaying the Word document inside Visual Studio as a designer that you can drag and drop controls on. These tools make it super easy to customize the task pane, and they greatly reduce the amount of code (and time) needed to create a smart tag solution in Word or Excel. VSTO enables you to create customizations for a specific document (document-level customi-zations). With the second edition (VSTO 2005 SE), you can create add-ins (application-level customizations). You will learn more about VSTO 2005 SE in Chapter 14. The remainder of this chapter discusses how you can create document-level customizations in Word using VSTO.

You can access all the properties, methods, and events of the Word object model using VSTO (as you can when using VBA). These objects are exposed in VSTO through Word's primary interop assembly (Microsoft.Office.Interop.Word.dll). There are some differences, however, in how you access these objects from VSTO. For example, you are probably used to accessing the Range object directly in VBA. In VSTO, the Range object isn't defined, so you must fully qualify it, as shown in Listing 6.1.

Listing 6.1. Accessing the Range object in VBA versus VSTO ' VBA

Dim myRange as Range myRange = ActiveDocument.Range(0, 0) myRange.Text = "Hello world"

' Visual Basic 2005 Dim myRange As Word.Range myRange = Me.Range(0, 0) myRange.Text = "Hello world"

Using VBA to customize Word, you have probably made extensive use of the ActiveDocument object to control objects within the currently active document. Because VSTO solutions are document-specific (typically, you are working only with the document that is associated with the code), you can instead use a reference to ThisDocument. If you're writing code within the ThisDocument class, you can access all its members by using the Me keyword. For example, if you wanted to assign text to a particular range in the document, such as the start of the document, you could write Me.Range instead of ActiveDocument.Range, as shown in Listing 6.1. For more information about the differences between VBA and Visual Basic 2005, see Chapter 4.

There are several ways you can use VBA to learn about the methods, properties, and events of Word's object model. You can look at the objects in the Object Browser of the VBE, or you can use IntelliSense within the VBE. These features, along with more advanced features, are also available in Visual Studio when you're creating Word solutions in VSTO. These features are described in more detail in Chapter 2.

VBA also gives you another way to become familiar with the object model: macro recording. The macro recorder records actions taken within an application and translates the actions into code. You can use the macro recorder to generate code that you later modify, or to learn about which object you should use to programmatically perform an action. VSTO does not provide this functionality in Visual Studio. However, you can always open Word outside Visual Studio, record a macro, and then translate the resulting code from VBA to Visual Basic 2005.

The VBA code that you generate through macro recording, or code that you write directly in the VBE, can be associated with a button on the toolbar or menu (known as CommandBars) via the Customize menu located in Word's Tools menu. You can click the Macros option and then drag a macro listed in the dialog box to an existing menu or toolbar.

Figure 6.1 shows a macro named BoldItalic in the Customize dialog box. Because code in a VSTO solution is not stored within the document or the template, you cannot associate its code with a CommandBar button in this same way. In fact, you cannot access the Customize dialog box from VSTO. Instead, you must customize toolbars and menus programmatically.

0 0

Post a comment