This post explains how to make a ribbon tab in Visio 2010 ribbon document-specific. Means, activate a custom ribbon tab only for a specific document.
This article discusses the implementation of a document-specific tab in a COM add-in; also, it focuses on details you might be interested in if you e.g. program unmanaged C++. Note that you could create a custom document-specific ribbon by other means, e.g. by embedding your custom interface in a document template using new Visio 2010 property Document.CustomUI
Nevertheless, one of the ways to it is
- Create a Visio add-in that supports ribbon (as described in previous post)
- Add onLoad event to the ribbon XML. This will allow you to obtain the ribbon manager.
<customUI onLoad="OnLoad" ... >
- In the event handler for the onLoad event, save passed-in ribbon manager. This is needed to force update of the ribbon.
// this function provides us with IRibbonUI object, called when XML is loaded public void OnLoad(object obj) { vsoRibbonUI = (Office.IRibbonUI) obj; }
- For the custom tab, add visibility callback function. This callback is called when Visio
- decides if the tab shall be shown or not. In the XML this looks like:
<tab id="Tab1" label="MyAddin" getVisible="IsMyTabVisible" >
- Create a callback handler that returns if the tab shall be shown or not. In my case (for simplicity), I just check if the active document has a user-defined cell “User.IsMyDoc”
// callback for the tab visibility, return if our tab shall be visible public bool IsMyTabVisible(object control) { Visio.Document activeDoc = vsoApplication.ActiveDocument; bool show = activeDoc != null && activeDoc.DocumentSheet.get_CellExists("User.IsMyDoc", 0) != 0; return show; }
- Add handlers to all events that might cause tab to show/hide. For a our document-dependent tab, these are “DocumentAdded”, “DocumentOpened”, “DocumentClosed”, and “Window activated” (in case you want to show/hide your tab when user switches to another window). From each of these handlers, just trigger the refresh for the ribbon:
void vsoApplication_DocumentCreated(Microsoft.Office.Interop.Visio.Document doc) { vsoRibbonUI.Invalidate(); }
That’s basically it; now, when a document that contains a cell named “User.IsMyDoc” is activated the tab is shown; otherwise this tab is hidden.
VisioRibbonTestCPP2.zip (19.9 KiB) VisioRibbonTestCS2.zip (17.9 KiB)