Importing Charts, Customizing the Ribbon, Starting Excel from Outlook

Often I see customers on forums asking how to display custom user interface in the Office Fluent ribbon under some specific conditions. For example, how to display a custom ribbon only for mail inspectors, or, how would one tell the inspector is in compose mode and not read mode. Browsing just the titles and summaries of existing code samples, such as those under Office UI Customization and Extending the User Interface in Outlook 2010, may not yield those samples that actually answer to such scenarios as well. Because of length restrictions, titles and summaries typically focus only on the core scenarios. So I think it’s worthwhile to point out some useful techniques in one of my recently published Visual How To, that you can easily apply in other common scenarios as well - customizing the ribbon, starting one Office application from another (in C#), and extending the status report automation scenario.

The Visual How To, Automating Chart Importation from Excel to an Email Message in Outlook 2010, describes a simple, core scenario of programmatically copying a chart from an Excel worksheet to an email message that’s being composed in Outlook. The sample code uses the Outlook, Excel, and Word object models. The automation is useful if you have to regularly create status email messages that involve charts in Excel workbooks. You can have an add-in that creates an email message, opens the workbook in Excel, then copies and pastes a chart into the email message.

Aside from showing the core scenario, there are a few secondary aspects of the Visual How To that I would like to highlight.

Customizing the ribbon

The custom user interface is a button “Copy Exel Chart” in its own group “MyGroup” and own tab “MyTab” in the Office Fluent ribbon.


In this example, I want the custom tab to be displayed in the Outlook inspector ribbon, only when the user is composing an email message in an inspector.

To do that, first, implement a custom even handler for the NewInspector event of the Inspectors object, to refresh the inspector ribbon before a new inspector is opened, by calling the IRibbonUI.Invalidate method.

Second, implement the IRibbonExtensibility.GetCustomUI method so that Outlook loads the XML for the custom ribbon only when the context is the mail inspector in compose mode. The context is reflected by the ribbon identifier value Microsoft.Outlook.Mail.Compose. For more information about ribbon identifiers, see Extending the User Interface in Outlook 2010.

Third, in the XML that specifies the custom UI, set the getVisible attribute of the custom tab to a callback method, MyTabInspector_GetVisible.

Lastly, implement the MyTabInspector_GetVisible callback method to make sure that the custom UI is indeed displayed in the following conditions:

  • The Context property of the tab control, which represents the current window, is an Outlook Inspector object.
  • The item in the current inspector is a MailItem object.
  • The Sent property of the MailItem object in the current inspector is false, indicating that the mail is in compose mode.

Starting Excel from Outlook in C#

Before copying a chart from Excel, the sample code first checks if Excel is running. If Excel is running, the sample code uses that instance of Excel. If Excel is not already running, then the sample code starts Excel by creating an instance.

To check if Excel is running on the customer’s computer, the code uses the GetActiveObject method of the Marhsal class. If Excel is not already running, the code uses the C# new keyword to create a new instance of Excel. Note that in Visual Basic, you can call the CreateObject function to start one Office application from another; however, C# does not support a CreateObject function.

Extending the automation in Excel

If the core scenario of automating chart importation interests you, you might also want to read the “Practical Extensions” section of the Visual How To, to see how you can use the Excel object model to update PivotTable data from an OLAP or SQL data source, and then update a PivotChart in Excel before copying the chart to Outlook.