
374
|
Chapter 11, Visual Studio Tools for Office
#85 Display a Windows Form from Excel 2003
HACK
Next, you need to create a SqlConnection, 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 ear-
lier 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( )
{
// Set up the Click events for the command bar buttons.
CBarButtonEvent =
new Office._CommandBarButtonEvents_ClickEventHandler
(CBarButton_Click);
CBarButton.Click += CBarButtonEvent;
}
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( )
{
SetupCommandBar( );
WireUpEvents( );
}
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
HACK
#85
Display a Windows Form from Excel 2003 Hack #85
Create a graphical application to gather information for Excel.
In this hack, you’ll learn how to make an Excel workbook pop up a Win-
dows Form application to collect ...