Author Topic: How to add macros to the Quick Access Toolbar (QAT)?  (Read 142 times)

0 Members and 1 Guest are viewing this topic.

BDein

  • Newbie
  • *
  • Posts: 1
How to add macros to the Quick Access Toolbar (QAT)?
« on: March 24, 2020, 05:49:11 AM »
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.
« Last Edit: March 24, 2020, 06:25:35 AM by Paul Herber »

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1707
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: How to add macros to the Quick Access Toolbar (QAT)?
« Reply #1 on: March 24, 2020, 06:25:18 AM »
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
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
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}");
}
}




« Last Edit: March 24, 2020, 07:31:43 AM by Visio Guy »
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

  • Full Member
  • ***
  • Posts: 174
Re: How to add macros to the Quick Access Toolbar (QAT)?
« Reply #2 on: March 31, 2020, 04:50:45 AM »
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".

Code
<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

Code
<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.