
372
|
Chapter 11, Visual Studio Tools for Office
#84 Create a Command Bar in Word 2003
HACK
Now that the method to create the command bar is complete, you will need
to call the method to initialize the command bar’s creation when a new doc-
ument is created:
protected void ThisDocument_New( )
{
SetupCommandBar( );
}
Now, test your new command bar by executing the application. Visual Stu-
dio should start an instance of Word, create a new document from your
blank template, and finally raise the
ThisDocument_New event, which will cre-
ate your command bar as shown in Figure 11-3.
Handle the Command Bar Events
Now that you have a command bar in Word, you need to listen for the
events that the command bar raises. In this section, you will learn how to
wire up the command bar events to your custom code.
The first order of business is to create a class-level delegate for the events
you want to handle:
public class OfficeCodeBehind
{
private Office._CommandBarButtonEvents_ClickEventHandler
CBarButtonEvent;
...
Second, you will need to create a method containing the code you want to
run when the command bar raises the event. In this code, you will be taking
the combo box selection and looking up the customer record to type the
address into the Word document:
private void CBarButton_Click(
Office.CommandBarButton btn,
Figure 11-3. Command bar in Word