Freigeben über


Small Changes: VSTO host item and host controls & InnerObject

In VSTO, we have host item bases (the base classes for the Document, Workbook, Sheet, and Chart project items) and host item controls (member variables of these base classes that are created when you add bookmarks, named ranges, list objects etc. to Word and Excel).

For example, there is a control in VSTO called "Microsoft.Office.Tools.Excel.NamedRange" that has all the properties, methods, and events of Microsoft.Office.Interop.Excel.Range. Prior to RTM, if you looked at the type definition of NamedRange, you would see that the type definition says that it implements the Microsoft.Office.Interop.Excel.Range interface. We discovered that this would cause VSTO 2.0 customizations to not work well with future versions of Office. So to make sure that VSTO 2.0 customizations will work in the future, we removed from the type definition our claim that we implement the Microsoft.Office.Interop.Excel.Range interface--but we left everything else. That is, the NamedRange control still has all the properties, methods, and events of Microsoft.Office.Interop.Excel.Range, it just doesn't claim that it implements the interface in the type definition anymore.

The impact on you is that when you try to pass a NamedRange to one of the 20 or so properties and methods in the Excel object model that take a Microsoft.Office.Interop.Excel.Range (like the Union method for example) you have to use the NamedRange controls "InnerObject" property which returns a Microsoft.Office.Interop.Excel.Range object, effectively returning the inner Microsoft.Office.Interop.Range object that NamedRange aggregates and calls through on when you call a property, method, or event of NamedRange:

Application.Union(myNamedRange1.InnerObject, myNamedRange2.InnerObject ...);

You also can't cast a NamedRange any more to a Microsoft.Office.Interop.Excel.Range, instead use InnerObject again.

Microsoft.Office.Interop.Excel.Range myRange = myNamedRange.InnerObject;

This applies to other controls as well like Microsoft.Office.Tools.Word.Bookmark, Microsoft.Office.Tools.Word.Document, Microsoft.Office.Tools.Word.XMLNode, Microsoft.Office.Tools.Word.XMLNodes, Microsoft.Office.Tools.Excel.Worksheet, Microsoft.Office.Tools.Excel.Workbook, Microsoft.Office.Tools.Excel.ListObject, Microsoft.Office.Tools.Excel.XMLMappedRange, Microsoft.Office.Tools.Excel.ChartObject, etc.

Comments

  • Anonymous
    August 23, 2005
    Today, Eric Carter blogged about Small Changes: VSTO host item and host controls & InnerObject. Basically,...
  • Anonymous
    September 27, 2005
    Eric,

    How do we migrate a VSTO add-in from VS2005 B2 to VS2005 RC compatable code?
  • Anonymous
    September 27, 2005
    So, Code like this is not best-practice in a VSTO RC Outlook add-in?

    Imports Outlook = Microsoft.Office.Interop.Outlook '<Microsoft.Office.Interop.Outlook
    Imports Office = Microsoft.Office.Core
    Imports System.Text
    Imports SysInfo = System.Windows.Forms.SystemInformation
    Imports Microsoft.Reporting.WinForms


    Friend Class MJContactBuilder

    'Private WithEvents _app As Outlook.Application
    Friend _app As MJ_Outlook_EntContacts.ThisApplication 'Outlook.Application


    Private _MJContactsView As Outlook.View

    Friend _OutlookContact As Outlook.ContactItem
    Private _OutlookSelection As Outlook.Selection