Microsoft.Office.Tools.Excel

This namespace contains a set of types that extend and support the Microsoft Excel object model
This includes the following types:
WorkbookBase and WorksheetBase classes - These are base classes for the ThisWorkbook and SheetN classes in document-level solutions
Workbook and Worksheet interfaces - These can be created at run-time in application-level solutions
Types used for SmartTag functionality
Various helper types, such as event arguments and delegates
Extended Objects - Host Items and Controls



You can access these item directly without having to traverse the Office object model.
Objects in this namespace are often referred to as VSTO objects.
VSTO supports adding both host items and host controls dynamically.
These are added to the Worksheet Controls collection
Excel controls tab of the toolbox in Visual Studio
Only the controls that can be added at design time are displayed on the toolbox


Why Bother

In document level customisations you might want to run some code when the user interacts with an object in your document level solution, but not when the user interacts with a similar object in another file.
When you have a Native object you can test whether that object has been extended using the GetVstobject and HasVstoObject methods.



InnerObject

If you ever need to pass the aggregate to an Excel object model method that requires the real com Excel object, you can obtain this by using the "InnerObject" property
this.NamedRangeItem.InnerObject



Host Items

Excel has three types of host items and their behaviour is all slightly different
Workbook
Worksheet
Chartsheet


Host Controls

The host items and host controls are in the Microsoft.Office.Tools.Excel namespace.
Host controls are built on top of native office objects and provide additional data binding and events
Native objects can be found in the Microsoft.Office.Interop.Excel namespace
Host controls can be found in the Microsoft.Office.Tools.Excel namespace
Worksheet - ListObject
Worksheet - NamedRange
Worksheet - Chart
Worksheet - XMLMappedRange
Worksheet - OLEObject


For a solution that dynamically creates host items in Excel, you must make sure that users of your solution have the "Trust access to Visual Basic project object model" checked

object temp = this.VBProject  //force VBA to initialize 

Using Document features in Application Level Add-ins

Starting with Visual Studio 2008 SP1 you can get a VSTO host item object for a document.
Once you have this host item you are able to use the Controls collection to add individual host controls.



If you add host controls at design-time and then the user accidentally deletes one and saves the document then a ControlNotFoundException will be generated the next time an event is listened for or a method is called.



It is difficult to prevent users from accidentally deleting controls unless the document is locked
It is possible to try and prevent design-time controls being deleted at run-time though.


Removing Controls

There are four ways to remove controls dynamically:

Controls.Delete 
ControlsCollection.Remove(control)
ControlsCollection.Remove(name)
ControlsCollection.RemoveAt(index)


Saving Host Controls

What happens if you add a host control dynamically and then the document is saved.
Host controls are part of the document so they will be saved although no information will persist.
If you want to reuse them you will have to reconnect the controls by adding them back to the ControlsCollection.


The ControlsCollection Add method lets you reconnect an existing control without creating a new on in the document.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext