Hosting Controls

Both Word and Excel have host items that function as containers for controls and code. A host item is essentially a proxy object that represents a physical document within either application. These are key to document-level customizations. For Word, we have the Microsoft.Office.Tools.Word.Document object, and for Excel, we have the Microsoft.Office.Tools.Excel.Worksheet object. Within Visual Studio, we build functionality using these host items through the use of designers. Each host item can host both Windows Forms controls and native Office controls.


Note

There is actually a third host item that represents an Excel workbook: Microsoft.Office.Tools.Excel.Workbook. It is a host item for enabling workbook-level customization, but it is not an actual controls container. Instead, Workbook functions as a component tray and can accept components such as a DataSet.


Windows Forms Controls

You can add Windows Forms controls onto the document design surface just as if you were designing a Windows Forms application. In this example, we use an Excel workbook. The Excel 2010 Workbook project template automatically adds an .xslx file to our project, which includes three worksheets, each represented by its own class. (These are the host items we discussed previously.) These sheets have defined events for startup and shutdown, enabling us to perform work as the worksheet is first opened or closed.

The design surface for the worksheet looks identical to the worksheet in Excel. From here, we can add Windows Forms controls to the worksheet by using the Visual Studio Toolbox, and we can implement code in the code-behind file to customize the action of those controls. Figure 22.15 shows a workbook designer in the IDE with a few controls added.

Image

FIGURE 22.15 Adding controls to an Excel spreadsheet.


Note

Creating an Office document project requires that your system allow access to the Microsoft Office Visual Basic for Applications project system. Normally, this type of access is disabled for security reasons. If access is disabled, Visual Studio prompts you to enable it before creating your Office project.


Host Controls

Host controls is the term applied to native Office controls. These controls actually extend objects found in the Word or Excel object models to provide additional capabilities such as event handling and data binding. Building out a document using host controls follows the same process as with Windows Forms controls. With a document-level project loaded, you see a tab in the Visual Studio Toolbox that stores the host controls for the specific application that is targeted. For Excel, there is an Excel Controls tab, and for Word, there is a Word Controls tab.

Table 22.2 itemizes the available host controls for both Excel and Word.

Image

TABLE 22.2 Microsoft Office Extension Points

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset