Customize Ribbon xml at Run Time Outlook code example

Customize Ribbon xml at Run Time Outlook code example

Ribbon XML Manipulation at Run Time

You can write code to show, hide, and modify the Ribbon xml, and enable users to run the code from controls in a custom task pane, actions pane, or Outlook form region. You can access the Ribbon by using the Globals class. For Outlook projects, you can access the Ribbons that appear in a specific Outlook Inspector or Outlook Explorer window.

Globals Class

  1. You can use the Globals class to access the Ribbon in a document-level project or VSTO Add-in project from anywhere in the project.
  2. For more information about the Globals class, see Global Access to Objects in Office Projects.
  3. The following example uses the Globals class to access a custom Ribbon named Ribbon1 and set the text that appears on a combo box on the Ribbon to Hello World.

Code example

private void Access_All_Ribbons_Globals()
{
	Globals.Ribbons.Ribbon1.comboBox1.Text = "Hello World";
}

Outlook Inspector Window

You can access a collection of Ribbons that appear in Outlook Inspectors. An Inspector is a window that opens in Outlook when users perform certain tasks, such as creating e-mail messages. To access the Ribbon of an Inspector window, call the Ribbons property of the Globals class and pass in an T:Microsoft.Office.Interop.Outlook.Inspector object that represents the Inspector.

The following example gets the Ribbon collection of the Inspector that currently has focus. This example then accesses a Ribbon named Ribbon1 and sets the text that appears on a combo box on the Ribbon to Hello World.

Code example

private void Access_Ribbons_By_Inspector()
{
	ThisRibbonCollection ribbonCollection = 
		Globals.Ribbons
			[Globals.ThisAddIn.Application.ActiveInspector()];
	ribbonCollection.Ribbon1.comboBox1.Text = "Hello World";
}

Outlook Explorer

You can access a collection of Ribbons that appear in an Outlook Explorer. An Explorer is the main application user interface (UI) for an instance of Outlook. To access the Ribbon of an Explorer window, call the Ribbons property of the Globals class and pass in an T:Microsoft.Office.Interop.Outlook.Explorer object that represents the Explorer.

The following example gets the Ribbon collection of the Explorer that currently has focus. This example then accesses a Ribbon named Ribbon1 and sets the text that appears on a combo box on the Ribbon to Hello World.

Code example

private void Access_Ribbons_By_Explorer()
{
	ThisRibbonCollection ribbonCollection =
		Globals.Ribbons
			[Globals.ThisAddIn.Application.ActiveExplorer()];
	ribbonCollection.Ribbon1.comboBox1.Text = "Hello World";
}

Next>>Excel COM Error

Leave a Reply

Your email address will not be published. Required fields are marked *