Share via


Office Solutions Programming Model

The Microsoft Office object model exposes a variety of objects that you can program against. Usually when you use Automation to program against Microsoft Office applications by using managed code, you write code directly against the primary interop assemblies. In Visual Studio Tools for Office solutions, you also write code directly against host items in your project.

Visual Studio Tools for Office also extends some of the objects in Microsoft Office Word and Microsoft Office Excel by adding events and data binding capabilities and exposing them as controls that you can add to a document in the same way you can add controls to a Windows Form. These extended controls are called host controls.

Using Host Items

Host items provide the Startup and Shutdown events, which are entry points for your code. To initialize your add-in, you can add code to the Startup event handler. To clean up resources used by your add-in, you can add code to the Shutdown event handler. For more information, see Visual Studio Tools for Office Project Events.

Host items also provide entry points into the object model of the host application. Projects for Excel and Word provide host items that resemble classes in the host application's object model. For example, the Microsoft.Office.Tools.Word.Document host item class in a Word document project provides the same members as the Document class in the Word object model. For more information, see Host Items and Host Controls Overview.

Application-level projects provide the AddIn host item. This host item does not derive from a class in the host application's object model. Instead, it provides the Application field, which returns an object that represents the current instance of the host application. For more information, see Programming Application-Level Add-Ins.

Using Host Control Events and Data Binding

Host controls have programmable events. For example, a native Excel Range does not have any events, but you can program against the Change event of a NamedRange host control. For more information, see Walkthrough: Programming Against Events of a NamedRange Control.

You can code against these events the same way you use events of controls on Windows Forms. For more information, see Events Overview (Windows Forms).

Host controls also have data binding capability that works in a way that is similar to controls on Windows Forms. Host controls enable you to do simple data binding or complex data binding. For more information, see Binding Data to Controls in Office Solutions and Data Binding and Windows Forms.

Programming Against Data in Document-Level Projects

Data in document-level projects can be stored separately from the document in an embedded data island, in a way similar to the way that ASP.NET Web pages store data. By using an embedded data island to store data, you can access the data without starting Word or Excel to instantiate the document, and without using the object model of the application. For more information, see Data in Document-Level Customizations.

Schema-Based Programming

The recommended way to access and manipulate data in Office projects is to create typed datasets to hold the data you are using. With schema-based programming you can define your own object model and program in a more intuitive way, using business-oriented code instead of Office-oriented code. You can concentrate on the solution rather than on the Office object model. For more information about typed datasets, see Datasets in Visual Studio Overview.

Server-Side Programming

Because data is stored in an embedded data island, you can directly access the data that is associated with documents stored on a server without starting the Microsoft Office application. For more information, see Accessing Data in Documents on the Server.

See Also

Concepts

Programming Document-Level Customizations

Programming Application-Level Add-Ins

Accessing Data in Documents on the Server

Host Items and Host Controls Overview

Other Resources

Controls on Office Documents

Data in Office Solutions