Author Topic: Custom Ribbon with VBA in Visio Stencil  (Read 4110 times)

0 Members and 1 Guest are viewing this topic.

Thomas Winkel

  • Full Member
  • ***
  • Posts: 180
Custom Ribbon with VBA in Visio Stencil
« on: March 28, 2016, 10:34:22 AM »
Hi,

please find attached a stencil that will draw a custom Ribbon "Visio-Tools" on load.
The XML code that defines the Ribbon is stored in the stencils document ShapeSheet "User.Ribbon".
You can export, modify and import the XML code with a mouse click.
The Ribbon will refresh automatically, no restart required.

User settings (XMLPath and reloadOnImport) are stored in the Windows Registry.
There is a button to remove this Registry settings.

The code has been derived from here:
http://blog.bvisual.net/2011/03/14/how-to-run-vba-macros-from-a-ribbon-button-in-visio-2010/
But it is much more comfortable to create the XML in your favorite editor (in my case Notepad++) than with code.

Feel free to use this stencil as start-up for your own projects.
Please give feedback if there is something to improve.

Regards,
Thomas

phillip_141

  • Jr. Member
  • **
  • Posts: 22
Re: Custom Ribbon with VBA in Visio Stencil
« Reply #1 on: July 29, 2016, 12:36:37 PM »
How can I view the shapsheet of the stencil? I figured it would be the same as  viewing it for a document, but the entire shape  design section of the developer ribbon is greyed out when I open just the stencil. The reason I ask is because I wanted to add this code to an existing stencil I use, but kept getting xml import errors. I can get the original "Visio tools" file to accept the xml file I have edited, but my stencil hits an error at this line:

ThisDocument.DocumentSheet.Cells("User.Ribbon").FormulaU = Chr(34) & XML & Chr(34)

which is from the "importXML()" sub.

Any help you could provide would be awesome.

Thomas Winkel

  • Full Member
  • ***
  • Posts: 180
Re: Custom Ribbon with VBA in Visio Stencil
« Reply #2 on: July 29, 2016, 01:23:22 PM »
Hi,

the problem is, that the code tries to access an user cell "User.Ribbon" that does not exist in your stencil.
So you have to create it by hand:
* Right click on stencil: Edit Stencil
* Right click on stencil: Drawing Explorer Window
* Right click on root in Drawing Explorer Window: Show ShapeSheet
* Right click in ShapeSheet: Insert Section -> User defined cells

Here is a video:
https://youtu.be/hzWDNw9dz8k

Regards,
Thomas

phillip_141

  • Jr. Member
  • **
  • Posts: 22
Re: Custom Ribbon with VBA in Visio Stencil
« Reply #3 on: August 04, 2016, 10:54:00 AM »
Ah I see. Thank you! I'm sure I can make use of this in the future! Nice ribbon code by the way. Managed to make lots of changes and expand the ribbon to suit my needs. I appreciate your assistance!

kedas

  • Jr. Member
  • **
  • Posts: 42
Re: Custom Ribbon with VBA in Visio Stencil
« Reply #4 on: October 30, 2017, 06:35:20 AM »
Hi,

is it possible to call macros inside the open document from the vss ribbon button?
I like to update the ribbon by opening a vss after opening a document but I can't seem to call subs from that document.

Thanks
« Last Edit: October 30, 2017, 07:15:36 AM by kedas »

kedas

  • Jr. Member
  • **
  • Posts: 42
Re: Custom Ribbon with VBA in Visio Stencil
« Reply #5 on: October 31, 2017, 12:31:20 PM »
I have this file attached, the macros don't fire (macros are just msgbox now to see if it works)
When pressing Ctrl+Shift+C it will change the Ribbon, only my 2 buttons.
When pressing Ctrl+Shift+R it will reset the Ribbon with my 2 extra butons

Do you know why they don't work??
I basically used the code of the stencil example but put it in a .vsd file (I don't use set CustomUI close reopen)

edit, found it i thought you could choose the name 'Button_OnAction'
« Last Edit: October 31, 2017, 01:26:33 PM by kedas »

DJHarris71

  • Jr. Member
  • **
  • Posts: 11
Re: Custom Ribbon with VBA in Visio Stencil
« Reply #6 on: February 02, 2018, 08:24:36 AM »
Hello,
I know this is post is a bit old, but this is the solution I have been looking for!  Thanks very much!
I did make mine a bit simpler and chose to create a string in VBA for the XML, and got rid of all the subs that move the string in and out of the shape data.  It works very well.


    ribbonXML = "<customUI onLoad=""OnRibbonLoad"" xmlns=""http://schemas.microsoft.com/office/2009/07/customui"">"
    ribbonXML = ribbonXML + "   <ribbon>"
    ribbonXML = ribbonXML + "       <tabs>"
    ribbonXML = ribbonXML + "           <tab id=""Tab1"" label=""Tab1"">"
    ribbonXML = ribbonXML + "               <group id=""Group1"" label=""Group1"" >"
    ribbonXML = ribbonXML + "                   <button id=""Button1"" label=""Button1"" supertip=""Button1"" size=""large"" imageMso=""MailMergeGoToNextRecord"" onAction=""Button_OnAction""/>"
    ribbonXML = ribbonXML + "                   <button id=""Button2"" label=""Button2"" supertip=""Button2"" size=""large"" imageMso=""SignaturesLoading"" onAction=""Button_OnAction""/>"
    ribbonXML = ribbonXML + "               </group>"
    ribbonXML = ribbonXML + "           </tab>"
    ribbonXML = ribbonXML + "       </tabs>"
    ribbonXML = ribbonXML + "   </ribbon>"
    ribbonXML = ribbonXML + "</customUI>"


However - it seems anytime I try to do something not provided in your example, my ribbon will not load.  Here are the three things I tried:
1) adding getVisible=""getVisible"" to the tabs and groups. 
2) adding getEnabled=""getEnabled"" to the tabs and groups. 
3) adding an editBox

All the above use the standard method (add the action in the ribbon with the callback sub name, add the callback sub) as shown below:


{the gobal variable}
Public objRibbonVisible As Boolean
'
{inside the ribbon string}
ribbonXML = ribbonXML + "               <group id=""Group1"" label=""Group1"" getVisible=""GetVisible"">"

{The callback sub}
Public Sub GetVisible(control As IRibbonControl, visible)
    visible = objRibbonVisible
    objRibbon.Invalidate
End Sub

Is there something I am missing??
« Last Edit: February 07, 2018, 10:11:43 AM by DJHarris71 »