Global Access to Objects in Office Projects
When you create an Office project, Visual Studio automatically generates a class named Globals in the project. You can use the Globals class to access several different project items at run time from any code in the project.
Applies to: The information in this topic applies to document-level projects and VSTO add-in projects. See Features Available by Office Application and Project Type.
Globals is a static class that keeps references to certain items in your project. By using the Globals class, you can access the following items from any code in the project at run time:
The
ThisWorkbookandSheetn classes in an Excel workbook or template project. You can access these objects by using theGlobals.ThisWorkbookandSheetn properties.The
ThisDocumentclass in a Word document or template project. You can access this object by using theGlobals.ThisDocumentproperty.The
ThisAddInclass in an VSTO Add-in project. You can access this object by using theGlobals.ThisAddInproperty.All Ribbons in your project that you customized by using the Ribbon Designer. You can access the Ribbons by using the
Globals.Ribbonsproperty. For more information, see Accessing the Ribbon at Run Time.All Outlook form regions in an Outlook VSTO Add-in project. You can access the form regions by using the
Globals.FormRegionsproperty. For more information, see Accessing a Form Region at Run Time.A factory object that enables you to create Ribbon controls, and host items at run time in projects that target the .NET Framework 4 or the .NET Framework 4.5. You can access this object by using the
Globals.Factoryproperty. This object is an instance of a class that implements one the following interfaces:
For example, you can use the Globals.Sheet1 property to insert text into a NamedRange control on Sheet1 when a user clicks a button on the actions pane in a document-level project for Excel.
Code that attempts to use the Globals class before the document or VSTO Add-in is completely initialized might throw a run time exception. For example, using Globals when declaring a class-level variable might fail because the Globals class might not be initialized with references to all of the host items before the declared object is instantiated.
|
Accessing the Ribbon at Run Time
Accessing a Form Region at Run Time
Host Items and Host Controls Overview
Document Host Item
Workbook Host Item
Worksheet Host Item
Writing Code in Office Solutions