Dela via


Programming Document-Level Customizations

When you extend Microsoft Office Word or Microsoft Office Excel by using a document-level customization, you can perform the following tasks:

  • Automate the application by using its object model.

  • Add controls to the surface of the document.

  • Call Visual Basic for Applications (VBA) code in the document from the customization assembly.

  • Call code in the customization assembly from VBA.

  • Manage certain aspects of the document while it is on a server that does not have Microsoft Office installed.

  • Customize the user interface (UI) of the application.

Applies to: The information in this topic applies to document-level projects for the following applications: Excel 2007 and Excel 2010; Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.

Some aspects of writing code in document-level projects are different from other types of projects in Visual Studio. Many of these differences are caused by the way the Office object models are exposed to managed code. For more information, see Writing Code in Office Solutions.

For general information about document-level customizations and other types of solutions you can create by using the Office development tools in Visual Studio, see Office Solutions Development Overview.

Using the Generated Classes in Document-Level Projects

When you create a document-level project, Visual Studio automatically generates a class in the project that you can use to start writing your code. Visual Studio generates different classes for Word and Excel:

  • In document-level projects for Word, the class is called ThisDocument by default.

  • Document-level projects for Excel have multiple generated classes: one for the workbook itself, and one for each worksheet. By default, these classes have the following names:

    • ThisWorkbook

    • Sheet1

    • Sheet2

    • Sheet3

The generated class includes event handlers that are called when the document is opened or closed. To run code when the document is opened, add code to the Startup event handler. To run code just before the document is closed, add code to the Shutdown event handler. For more information, see Events in Office Projects.

Understanding the Design of the Generated Classes

In projects that target the .NET Framework 3.5, the generated classes derive most of their members and behavior from the following host item classes in the Visual Studio Tools for Office runtime: 

  • ThisDocument: derives from Microsoft.Office.Tools.Word.Document

  • ThisWorkbook: derives from Microsoft.Office.Tools.Excel.Workbook

  • Sheetn: derives from Microsoft.Office.Tools.Excel.Worksheet

Host items are types that are at the top of object model hierarchies in Office projects, and they extend the behavior of corresponding objects in the Office primary interop assemblies (PIAs). For more information about host items, see Host Items and Host Controls Overview.

In projects that target the .NET Framework 4, the host item types in the Visual Studio Tools for Office runtime are interfaces, so the generated classes cannot derive their implementation from them. Instead, the generated classes derive most of their members from the following base classes:

These base classes redirect all calls to their members to internal implementations of the corresponding host item interfaces in the Visual Studio Tools for Office runtime. For example, if you call the Protect method of the ThisDocument class, the Microsoft.Office.Tools.Word.DocumentBase class redirects this call to the internal implementation of the Microsoft.Office.Tools.Word.Document interface in the Visual Studio Tools for Office runtime. For more information about the differences in the Visual Studio Tools for Office runtime for projects that target the .NET Framework 3.5 and the .NET Framework 4, see Visual Studio Tools for Office Runtime Overview

Accessing the Object Model of the Host Application

To access the object model of the host application, use members of the generated class in your project. Each of these classes corresponds to an object in the object model of Excel or Word, and they contain most of the same properties, methods, and events. For example, the ThisDocument class in a document-level project for Word provides most of the same members as the Microsoft.Office.Interop.Word.Document object in the Word object model.

The following code example shows how to use the Word object model to save the document that is part of a document-level customization for Word. This example is intended to be run from the ThisDocument class.

Me.Save()
this.Save();

To do the same thing from outside the ThisDocument class, use the Globals object to access the ThisDocument class. For example, you can add this code to an actions pane code file if you want to include a Save button in the actions pane UI.

Globals.ThisDocument.Save()
Globals.ThisDocument.Save();

Because the ThisDocument class obtains most of its members from the Microsoft.Office.Tools.Word.Document host item, the Save method that is called in this code is really the Save method of the Microsoft.Office.Tools.Word.Document host item. This method corresponds to the Save method of the Microsoft.Office.Interop.Word.Document object in the Word object model.

For more information about using the object models of Word and Excel, see Word Object Model Overview and Excel Object Model Overview.

For more information about the Globals object, see Global Access to Objects in Office Projects.

Adding Controls to Documents

To customize the UI of the document, you can add Windows Forms controls or host controls to the document surface. By combining different sets of controls and writing code, you can bind the controls to data, collect information from the user, and respond to user actions.

Host controls are classes that extend some of the objects in the Word and Excel object model. For example, the Microsoft.Office.Tools.Excel.ListObject host control provides all of the functionality of the Microsoft.Office.Interop.Excel.ListObject in Excel. However, the Microsoft.Office.Tools.Excel.ListObject host control also has additional events and data binding capabilities.

For more information, see Host Items and Host Controls Overview and Windows Forms Controls on Office Documents Overview.

Combining VBA and Document-Level Customizations

You can use VBA code in a document that is part of a document-level customization. You can call VBA code in the document from the customization assembly, and you can also configure your project to enable VBA code in the document to call code in the customization assembly.

For more information, see Combining VBA and Document-Level Customizations.

Managing Documents on a Server

You can manage several different aspects of document-level customizations on a server that does not have Microsoft Office Word or Microsoft Office Excel installed. For example, you can access and modify data in the data cache of the document. You can also manage the customization assembly that is associated with the document. For example, you can programmatically remove the assembly from the document so that the document no longer runs your code, or you can programmatically attach an assembly to a document.

For more information, see Managing Documents on a Server by Using the ServerDocument Class.

Customizing the User Interface of Microsoft Office Applications

You can customize the UI of Word and Excel in the following ways by using a document-level customization:

For more information about customizing the UI of Microsoft Office applications, see Office UI Customization.

See Also

Concepts

Managing Documents on a Server by Using the ServerDocument Class

Getting Extended Objects from Native Office Objects in Document-Level Customizations

Writing Code in Office Solutions

Other Resources

Controls on Office Documents

Combining VBA and Document-Level Customizations