How to add macros to the Quick Access Toolbar (QAT)?

Started by BDein, March 24, 2020, 10:49:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

BDein

Hi There,

There are writing about how to add macros to the menus but I can't find any about how to add them to the QAT, does anyone know?
I'd like to use VBA for it, and load them when documents opens.

Visio Guy

#1
Hi BD,

Visio doesn't have an easy way to do this, like Excel (e.g. Adding a Macro to the Quick Access Toolbar).

I think it would be possible to write an add-in that could allow this--at least adding buttons to a custom ribbon group, but nobody has done it yet, as far as I know.

Visio doesn't have an Application.Run() method, but you can use Document.ExecuteLine.

The last trick would be getting the QAT or Ribbon to load new buttons when users made changes. I'm pretty sure it can be done with a custom ribbon group, but I'm not sure about dynamically loading stuff into the QAT.

I ran a bit of code to test out calling a macro from a simulated C# add-in -- it's a LINQPad query/script:


Code (csharp) Select
void Main()
{
/*
          Setup:                           
  ------------------------------     
  Test document has:           
  - A module named 'MTests'     
      - MTests has a proc named 'DoIt'
        */

_tryRunVbaProc("DoIt");         /* Works! */
        _tryRunVbaProc("MTests.DoIt");  /* Works! */
_tryRunVbaProc("Samsung");      /* Error! */
}

Code (csharp) Select
private void _tryRunVbaProc(string vbaProcName)
{
        /* This is the proc that actually tries to call the macro: */

        /* I'm using my own libraries to get the active document and
            check if it is good - you will need your own code here: */

        Vis.Document visDoc = OOP.GetActiveDocument();
if(visDoc.IsNullOrNotNormal()) return;

        /* The active document might not have the macro, so try-catch! */
try
{
visDoc.ExecuteLine(vbaProcName);
Debug.WriteLine($"Macro '{vbaProcName}' was executed!");
}
catch (Exception ex)
{
Debug.WriteLine($"Error attempting to execute macro:\n{ex.Message}");
}
}





For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

metuemre

You can embed a customUI.xml file in the visio file itself to add buttons to Quick Access Toolbar. This would be more useful if you are using Visio template files to create your drawings. Create a new folder named "userCustomization" in the visio file and put the customUI.xml file in it.

In Visio 2013 microsoft removed the page navigation buttons, so I added them to quick access toolbar with the xml code below. Each button calls a separate macro located in a stencil named "Auxiliary".


<mso:customUI xmlns:mso="http://schemas.microsoft.com/office/2006/01/customui">
<mso:ribbon>
<mso:qat>
<mso:documentControls>
<mso:button id="FirstPage" onAction="Auxiliary!PageNavigation.FirstPage" imageMso="FirstTile" label="First Page" visible="true" />
<mso:button id="PreviousPage" onAction="Auxiliary!PageNavigation.PreviousPage" imageMso="PreviousPage" label="Previous Page" visible="true" />
<mso:button id="NextPage" onAction="Auxiliary!PageNavigation.NextPage" imageMso="NextPage" label="Next Page" visible="true" />
<mso:button id="LastPage" onAction="Auxiliary!PageNavigation.LastPage" imageMso="LastTile" label="Last Page" visible="true" />
<mso:button id="PreviousWindow" onAction="Auxiliary!PageNavigation.PreviousWindow" imageMso="PreviousUnread" label="Previous View" visible="true" />
</mso:documentControls>
</mso:qat>
</mso:ribbon>
</mso:customUI>

You also need to add a line to .rels file in the _rels folder similar to below

<Relationship Id="rId6" Type="http://schemas.microsoft.com/office/2006/relationships/ui/userCustomization" Target="userCustomization/customUI.xml"/>

You can see the related QAT buttons on the attached screenshot.