This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge

In this article

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 .

How to use the Globals class

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 ThisWorkbook and Sheet n classes in an Excel workbook or template project. You can access these objects by using the Globals.ThisWorkbook and Sheet n properties.

  • The ThisDocument class in a Word document or template project. You can access this object by using the Globals.ThisDocument property.

  • The ThisAddIn class in a VSTO Add-in project. You can access this object by using the Globals.ThisAddIn property.

  • All Ribbons in your project that you customized by using the Ribbon Designer. You can access the Ribbons by using the Globals.Ribbons property. For more information, see Access 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.FormRegions property. For more information, see Access 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.Factory property. This object is an instance of a class that implements one the following interfaces:

  • Microsoft.Office.Tools.Factory

  • Microsoft.Office.Tools.Excel.Factory

  • Microsoft.Office.Tools.Outlook.Factory

  • Microsoft.Office.Tools.Word.Factory

    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.

    private void button1_Click(object sender, EventArgs e)
        Globals.Sheet1.namedRange1.Value2 = this.textBox1.Text;
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
        Globals.Sheet1.NamedRange1.Value2 = Me.TextBox1.Text
    End Sub
    

    Code that attempts to use the Globals class before the document or VSTO Add-in is 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.

    The Globals class is never initialized at design time, but control instances are created by the designer. This means that if you create a user control that uses a property of the Globals class from inside a user control class, you must check whether the property returns null before you try to use the returned object.

  • Access the Ribbon at run time
  • Access a form region at run time
  • Host items and host controls overview
  • Document host item
  • Workbook host item
  • Worksheet host item
  • Write code in Office solutions
  •