Share via


How to: Add and Remove Host Objects in an Add-in Project

You add host objects to add-in projects when developers add objects in the host application while they are developing add-ins. For example, in an application like Microsoft Office Word, when a developer adds a page to a document you can add a host object to the project that the developer can use to customize that page.

Host objects require host items that act as containers. Before you can add a host object, such as a page, you must first add a host item, such as a document. For more information, see Dynamically Creating and Modifying Host Items and Host Objects in an Add-in Project.

To add and remove host objects, get the host adapter of the Visual Studio Tools for Applications IDE. You can then use the host adapter to access a collection of project host items. 

The code used in these procedures is taken from the VstaDesignTimeIntegration.cs file of the ShapeAppDynamicProgrammingModelCSharp sample. For more information, see How to: Build and Run the ShapeAppDynamicProgrammingModelCSharp Sample.

To add a host object to a project

  1. Get the host adapter by using the Extender property of the Project instance.

    IVstaHostAdapter hostAdapter = 
        (IVstaHostAdapter)project.get_Extender("VSTAHostAdapter2007");
    
  2. Call the Add method of the HostObjectCollection that is associated with the host item.

    The following example adds a host object that represents a shape to a project in the ShapeApp sample application.

    void AddShapeHostObject(ShapeApp.Drawing drawing, IShape shape)
    {
        IVstaHostItem item = 
    this.hostAdapter.ProjectHostItems[drawing.Cookie].ProgrammingModelHostItem;
        item.HostObjects.Add(shape.Name,
            "Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp.IShape", drawing.Cookie + "$" + shape.Name);
    }
    

To remove a host object from a project

  1. Get the host adapter by using the Extender property of the Project instance.

    IVstaHostAdapter hostAdapter = (IVstaHostAdapter)project.get_Extender("VSTAHostAdapter2007");
    
  2. Call the Remove method of the HostObjectCollection that is associated with the host item. The following method removes a host object that represents a shape from a project in the ShapeApp sample application.

    void RemoveShapeHostObject(ShapeApp.Drawing drawing, IShape shape)
    {
        IVstaHostItem item = 
        this.hostAdapter.ProjectHostItems[drawing.Cookie].ProgrammingModelHostItem;
        IVstaHostObject hostObject = item.HostObjects[drawing.Cookie + "$" + shape.Name];
        item.HostObjects.Remove(hostObject);
    }
    

See Also

Tasks

How to: Add and Remove Host Objects in an Add-in Project

How to: Add and Remove Host Items in an Add-in Project

Walkthrough: Adding Host Items and Host Objects to an Add-in Project

Concepts

Dynamically Creating and Modifying Host Items and Host Objects in an Add-in Project

ShapeApp Samples (Visual Studio Tools for Applications)

Creating In-Process Hosts

Defining Entry Points and Other Proxy Changes