Hack 84. Create a Command Bar in Word 2003![]() You'll want to click it for no good reason at all, just knowing how cool it is that you're calling into managed code.In this hack, you will be creating a command bar item with a list of customers from the Northwind database.If your installation of the Visual Studio Tools for Office is complete, you should see some additional project types in Visual Studio's New Project dialog. I'll show you how to create a new Visual C# Project for a Microsoft Word template called CommandBar (as shown in Figure 11-1). Figure 11-1. New Project dialog![]() click OK and you'll be presented with the Microsoft Office Project Wizard (Figure 11-2). Since you are going to be working with a new document template, select the option to Create a New Document. If you want to change the name or location of the document template that will be created, you can change the corresponding text box.On the Security Settings tab, the wizard will automatically update your security settings to allow the managed code document to run. There is not space here to explain how to set all of the security settings, so until you can spend some time figuring out the Code Access Security model for the project, I suggest that you let the wizard make the necessary changes. When you click Finish, the wizard will create the required document templates and a code-behind class for the document. Figure 11-2. Microsoft Office Project Wizard![]() 11.3.1. Create the Command BarWhen the wizard has completed, you will be placed into the main code file named ThisDocument.cs. Most of your code will execute when a user creates a new instance of your document. This event is handled by the following method: protected void ThisDocument_New( )At this point, you are going to need to create some class-level variables to work with as you build your command bar. The main class is OfficeCodeBehind, so your code should look something like this: public class OfficeCodeBehindNext, you will need to write some code to create the different user interface elements that you plan to place on the command bar. In this example, you will need a combo listbox and a button. (You may need to change the connection string, highlighted in bold, to suit your SQL Server or MSDE installation.)
private bool SetupCommandBar( )In the preceding code, the first thing you need to create a command bar is a reference to the current instance of the Microsoft Word application you will be working in. Once you have this reference, you can create a new command bar by calling the Add method of the CommandBars collection.The next step is to create the drop-down combo box (Office.MsoControlType.msoControlDropdown). Once you have created the combo box, you can set the properties for how you want the control to appear to the user.After the combo box is created, you are ready to populate the control with values from a database. In this example, the data will be coming from the Northwind database on the SQL Server of the machine that is running the code. Since you will need only the CustomerID and CustomerName from the database, it is a good practice to select only that data. Since you will be using a SqlDataReader to get the data, you will be responsible for opening the connection in your code before executing your SqlCommand object. The ExecuteReader object will create a SqlDataReader object and pass the reference back to the variable we have declared in order to read the data. At this point, you have not read any data.The while loop begins reading the data by grabbing the next available row. As long as the SqlDataReader successfully returns data, the code that adds the item to the combo box executes. Unfortunately, in this example, the primary key that we need to use to identify the customer is a string-based key, so you cannot use it as the index parameter of the AddItem method. For now, you will make the key part of the display text and retrieve the key value when you handle the button click later. After reading the data, you need to close the SqlDataReader and the SqlConnection.Next, you need to create the button on the toolbar (Office.CommandBarButton) and set its display properties. Finally, show the command bar to the user with the Show method.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 document is created: protected void ThisDocument_New( )Now, test your new command bar by executing the application. Visual Studio should start an instance of Word, create a new document from your blank template, and finally raise the ThisDocument_New event, which will create your command bar as shown in Figure 11-3. Figure 11-3. Command bar in Word![]() 11.3.2. Handle the Command Bar EventsNow 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 OfficeCodeBehindSecond, 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(In the preceding code, you first create a variable for the current selection in Word. The next step is to take the selection from the combo box and split the text into an array containing two items.
SqlCommand, and SqlDataReader to retrieve the data from the database. Once you have retrieved the record, you will output the records to the Word.Selection variable that you created earlier using the TypeText method.Now that you have created the method to output the data record to your Word document, you need to wire that code into the button by attaching the delegate to your event handler: private void WireUpEvents( )One last order of business is to finish the document initialization process by updating the ThisDocument_New method to call the event wire-up code after the command bar has been created: protected void ThisDocument_New( )Now, build and test the application once again. This time, when you click the button on the command bar, the name and address for the customer will be typed into the Word document. Brian Sherwin |