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.
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext