Dela via


Programming Application-Level Add-Ins

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • 2007 Microsoft Office system

  • Microsoft Office 2003

For more information, see Features Available by Application and Project Type.

When you extend a Microsoft Office application by creating a Visual Studio Tools for Office add-in, you write code directly against the ThisAddIn class in your project. You can use this class to perform tasks such as accessing the object model of the Microsoft Office host application, customizing the user interface (UI) of the application, and exposing objects in your add-in to other Office solutions.

For general information about application-level add-ins and other types of solutions you can create by using Visual Studio Tools for Office, see Office Solutions Development Overview.

Writing Code in the Add-in Project

You can start writing your add-in code in the ThisAddIn class. Visual Studio Tools for Office automatically generates this class in the ThisAddIn.vb (in Visual Basic) or ThisAddIn.cs (in C#) code file in your add-in project.

There are two default event handlers in the ThisAddIn class. To run code when the add-in is loaded, add code to the ThisAddIn_Startup event handler. To run code just before the add-in is unloaded, add code to the ThisAddIn_Shutdown event handler. For more information, see Visual Studio Tools for Office Project Events.

The ThisAddIn class derives most of its features from the AddIn class. AddIn provides core functionality shared by all Visual Studio Tools for Office add-ins. For more information about the AddIn class, see AddIn Host Item.

Accessing the Object Model of the Host Application

To access the object model of the host application, use the Application field of the ThisAddIn class. This field returns an object that represents the current instance of the host application.

The following code example shows how to use the Application field to create a new workbook in an add-in for Microsoft Office Excel. This example is intended to be run from the ThisAddIn class.

Dim newWorkbook As Excel.Workbook = Me.Application.Workbooks.Add()
Excel.Workbook newWorkbook = this.Application.Workbooks.Add(System.Type.Missing);

To do the same thing from outside the ThisAddIn class, use the Globals object to access the ThisAddIn class. For more information about the Globals object, see Global Access to Objects in Visual Studio Tools for Office Projects.

Dim newWorkbook As Excel.Workbook = Globals.ThisAddIn.Application.Workbooks.Add()
Excel.Workbook newWorkbook = Globals.ThisAddIn.Application.Workbooks.Add(System.Type.Missing);

The following table lists the type of the return value for the Application field in each add-in project.

Host application

Return value type

Microsoft Office Excel

Application

Microsoft Office InfoPath

Microsoft.Office.Interop.InfoPath.Application

Microsoft Office Outlook

Microsoft.Office.Interop.Outlook.Application

Microsoft Office PowerPoint

Microsoft.Office.Interop.PowerPoint.Application

Microsoft Office Project

Microsoft.Office.Interop.MSProject.Application

Microsoft Office Visio

Microsoft.Office.Interop.Visio.Application

Microsoft Office Word

Application

Customizing the User Interface of Microsoft Office Applications

You can programmatically customize the UI of Microsoft Office applications by using an application-level add-in. For Microsoft Office 2003 applications, the UI features that you can customize include menus and toolbars. Applications in the 2007 Microsoft Office system have some different UI features that you can customize. These features include the Ribbon, custom task panes, and Outlook form regions. For more information, see Office UI Customization.

Visual Studio Tools for Office provides designers and classes that you can use to create custom task panes, Ribbon customizations, and Outlook form regions. These designers and classes help to simplify the process of customizing these features. For more information, see Custom Task Panes Overview, Ribbon Designer, and Creating Outlook Form Regions

If you want to customize one of these features in a way that is not supported by the Visual Studio Tools for Office classes and designers, you can also customize these features by implementing an extensibility interface in your add-in. For more information, see Customizing UI Features By Using Extensibility Interfaces.

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can also modify the UI of Word documents and Excel workbooks by generating host items that extend the behavior of documents and workbooks. This enables you to add managed controls and Visual Studio Tools for Office smart tags to documents and worksheets. For more information, see Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.

Calling Code in Application-Level Add-ins from Other Solutions

You can expose objects in your add-in to other solutions, including other Microsoft Office solutions. This is useful if your add-in provides a service that you want to enable other solutions to use. For example, if you have an add-in for Microsoft Office Excel that performs calculations on financial data from a Web service, other solutions can perform these calculations by calling into the Excel add-in at run time.

For more information, see Calling Code in Application-Level Add-ins from Other Solutions.

See Also

Tasks

Walkthrough: Calling Code in an Application-Level Add-in from VBA

How to: Create Visual Studio Tools for Office Projects

Concepts

Developing Office Solutions

AddIn Host Item

Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time

Calling Code in Application-Level Add-ins from Other Solutions

Customizing UI Features By Using Extensibility Interfaces

Architecture of Application-Level Add-Ins