Partilhar via


Accessing a Form Region at Run Time

Applies to

The information in this topic applies only to the following project types and versions of Microsoft Office. For more information, see Features Available by Office Application and Project Type.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2010

Use the Globals class to access form regions from anywhere within your Outlook project. For more information about the Globals class, see Global Access to Objects in Office Projects.

Applies to: The information in this topic applies to application-level projects for Outlook 2013 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

Accessing Form Regions That Appear in a Specific Outlook Inspector Window

To access all form regions that appear in a specific Outlook Inspector, call the FormRegions property of the Globals class and pass in an Inspector object that represents the Inspector.

The following example gets the collection of form regions that appear in the Inspector that currently has focus. This example then accesses a form region in the collection named formRegion1 and sets the text that appears in a text box to Hello World.

Private Sub Access_Form_Regions_ByInspector()
        Dim formRegions As WindowFormRegionCollection = Globals.FormRegions _
            (Globals.ThisAddIn.Application.ActiveInspector())
        formRegions.FormRegion1.textBox1.Text = "Hello World" 
    End Sub
private void Access_Form_Regions_ByInspector()
{
    WindowFormRegionCollection formRegions = 
        Globals.FormRegions
            [Globals.ThisAddIn.Application.ActiveInspector()];
    formRegions.FormRegion1.textBox1.Text = "Hello World";
}

Accessing Form Regions That Appear in a Specific Outlook Explorer Window

To access all form regions that appear in a specific Outlook Explorer, call the FormRegions property of the Globals class and pass in an Explorer object that represents the Explorer.

The following example gets the collection of form regions that appear in the Explorer that currently has focus. This example then accesses a form region in the collection named formRegion1 and sets the text that appears in a text box to Hello World.

Private Sub Access_Form_Regions_ByExplorer()
    Dim formRegions As WindowFormRegionCollection = Globals.FormRegions _
        (Globals.ThisAddIn.Application.ActiveExplorer())
    formRegions.FormRegion1.textBox1.Text = "Hello World" 
End Sub
private void Access_Form_Regions_ByExplorer()
{
    WindowFormRegionCollection formRegions =
        Globals.FormRegions
            [Globals.ThisAddIn.Application.ActiveExplorer()];
    formRegions.FormRegion1.textBox1.Text = "Hello World";
}

Accessing All Form Regions

To access all form regions that appear in all Explorers and all Inspectors, call the FormRegions property of the Globals class.

The following example gets the collection of form regions that appear in all Explorers and all Inspectors. This example then accesses a form region named formRegion1 and sets the text that appears in a text box to Hello World.

Friend Sub Access_All_Form_Regions()
    Dim formRegion As Microsoft.Office.Tools.Outlook.IFormRegion
    For Each formRegion In Globals.FormRegions
        If TypeOf formRegion Is FormRegion1 Then 
            Dim formRegion1 As FormRegion1 = _
                CType(formRegion, FormRegion1)
            formRegion1.textBox1.Text = "Hello World" 
        End If 
    Next formRegion
End Sub
internal void Access_All_Form_Regions()
{
    foreach (Microsoft.Office.Tools.Outlook.IFormRegion formRegion 
        in Globals.FormRegions)
    {
        if (formRegion is FormRegion1)
        {
            FormRegion1 formRegion1 = (FormRegion1)formRegion;
            formRegion1.textBox1.Text = "Hello World";
        }
    }

}

Accessing Controls on a Form Region

To access controls on a form region by using the Globals class, you must make the controls accessible to code outside of the form region code file.

Form Regions Designed in the Form Region Designer

For C#, change the modifier of each control that you want to access. To do this, select each control in the form region designer and change the Modifiers property to Internal or public in the Properties window. For example, if you change the Modifier property of textBox1 to Internal, you can access textBox1 by typing Globals.FormRegions.FormRegion1.textBox1.

For Visual Basic, you do not need to change the modifier.

Imported Form Regions

When you import a form region that was designed in Outlook, the access modifier of each control on the form region becomes private. Because you cannot use the form region designer to modify an imported form region, there is no way to change the modifier of a control in the Properties window.

To enable access to a control from outside the form region code file, create a property in the form region code file to return that control.

For more information about how to create properties in C#, see How to: Declare and Use Read/Write Properties (C# Programming Guide).

For more information about how to create properties in Visual Basic, see How to: Add Fields and Properties to a Class.

See Also

Tasks

Walkthrough: Designing an Outlook Form Region

How to: Add a Form Region to an Outlook Add-in Project

Walkthrough: Importing a Form Region That Is Designed in Outlook

How to: Prevent Outlook from Displaying a Form Region

Concepts

Guidelines for Creating Outlook Form Regions

Custom Actions in Outlook Form Regions

Associating a Form Region with an Outlook Message Class

Accessing the Ribbon at Run Time

Other Resources

Creating Outlook Form Regions