Editare

Partajați prin


Programmatic limitations of host items and host controls

Each host item and host control is designed to behave like a corresponding native Microsoft Office Word or Microsoft Office Excel object, with additional functionality. However, there are some fundamental differences between the behavior of host items and host controls and native Office objects at run time.

For general information about host items and host controls, see Host items and host controls overview.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel and Word. For more information, see Features available by Office application and project type.

Programmatically create host items

When you programmatically create or open a document, workbook, or worksheet at run time by using the Word or Excel object model, the item is not a host item. Instead, the new object is a native Office object. For example, if you use the Add method to create a new Word document at run time, it will be a native Document object rather than a Document host item. Similarly, when you create a new worksheet at run time using the Add method, you get a native Worksheet object rather than a Worksheet host item.

In document-level projects, you cannot create host items at run time. Host items can be created only at design time in document-level projects. For more information, see Document host item, Workbook host item, and Worksheet host item.

In VSTO Add-in projects, you can create Document, Workbook, or Worksheet host items at run time. For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at run time.

Programmatically create host controls

You can programmatically add host controls to a Document or Worksheet host item at run time. For more information, see Add controls to Office documents at run time.

You cannot add host controls to a native Document or Worksheet.

Note

The following host controls cannot be added programmatically to worksheets or documents: XmlMappedRange, XMLNode, and XMLNodes.

Understand type differences between host items, host controls, and native Office objects

For each host item and host control, there is an underlying native Microsoft Office Word or Microsoft Office Excel object. You can access the underlying object by using the InnerObject property of the host item or host control. However, there is no way to cast a native Office object to its corresponding host item or host control. If you try to cast a native Office object into the type of a host item or host control, an InvalidCastException is thrown.

There are several scenarios where the differences between the types of host items and host controls and the underlying native Office objects can affect your code.

Pass host controls to methods and properties

In Word, you cannot pass a host control to a method or property that requires a native Word object as a parameter. You must use the InnerObject property of the host control to return the underlying native Word object. For example, you can pass a Bookmark object to a method by passing the InnerObject property of the Bookmark host control to the method.

In Excel, you must use the InnerObject property of the host control to pass the host control to a method or property when the method or property expects the underlying Excel object.

The following example creates a NamedRange control and passes it to the AutoFill method. The code uses the InnerObject property of the named range to return the underlying Office Range that is required by the AutoFill method.

this.Range["A1"].Value2 = "Monday";
this.Range["A2"].Value2 = "Tuesday";

Microsoft.Office.Tools.Excel.NamedRange dayRange = 
    this.Controls.AddNamedRange(this.Range["A1", "A7"], "dayRange");
this.Range["A1", "A2"].AutoFill(dayRange.InnerObject, Excel.XlAutoFillType.xlFillDays);

Return types of native Office methods and properties

Most methods and properties of host items return the underlying native Office object upon which the host item is based. For example, the Parent property of a NamedRange host control in Excel returns a Worksheet object rather than a Worksheet host item. Similarly, the Parent property of a RichTextContentControl host control in Word returns a Document object rather than a Document host item.

Access collections of host controls

The Visual Studio Tools for Office runtime does not provide individual collections for each type of host control. Instead, use the Controls property of a host item to iterate through all managed controls (both host controls and Windows Forms controls) on the document or worksheet, and then look for items that match the type of the host control you are interested in. The following code example examines each control on a Word document and determines whether the control is a Bookmark.

foreach (object targetControl in this.Controls)
{
    Microsoft.Office.Tools.Word.Bookmark bookMark =
        targetControl as Microsoft.Office.Tools.Word.Bookmark;

    if (bookMark != null)
    {
        // Do some work with the bookmark here.
    }
}

For more information about the Controls property of host items, see Add controls to Office documents at run time.

The Word and Excel object models include properties that expose collections of native controls on documents and worksheets. You cannot access managed controls by using these properties. For example, it is not possible to enumerate each Bookmark host control in a document by using the Bookmarks property of a Document or the Bookmarks property of a Document. These properties include only the Bookmark controls in the document; they do not contain the Bookmark host controls in the document.