Accessing a Form Region at Run Time

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2007

For more information, see Features Available by Application and Project Type.

You can write code to show, hide, or modify controls on a form region, and enable users to run the code from other areas in your project by using the Globals class.

For more information about the Globals class, see Global Access to Objects in Visual Studio Tools for Office Projects.

Use the Globals class to access form regions that appear in Inspectors and Explorers from anywhere within the project. An Inspector is a window that opens in Outlook when users perform certain tasks, such as creating e-mail messages. An Explorer is a window that displays the contents of a folder that contains items such as e-mail messages, tasks, or appointments.

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

How to: Access the Outlook Item that Displays the Form Region

Concepts

Guidelines for Creating Outlook Form Regions

Custom Actions in Outlook Form Regions

Associating a Form Region with an Outlook Message Class

Creating Outlook Form Regions

Accessing the Ribbon at Run Time