News:

BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

Menu code in Visio stencil: .AddOnName not working

Started by hedstrompa, June 19, 2024, 09:47:08 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

hedstrompa

Inside a stencil(.vssm) I have a code to generate a menu with an item that should be able to execute a sub within the stencil. However, the menu option is greyed out in the drawing.

The two subs are in the same module called modExp in the same stencil.

Code to create Visio menu:
Public Sub GenMenu()

    Dim vsoUIObject As Visio.UIObject
    Dim vsoMenuSets As Visio.MenuSets
    Dim vsoMenuSet As Visio.MenuSet
    Dim vsoMenus As Visio.Menus
    Dim vsoMenu As Visio.Menu
    Dim vsoMenuItems As Visio.MenuItems
    Dim vsoMenuItem As Visio.MenuItem

    'Get a UI object that represents the Microsoft Visio built-in menus.
    Set vsoUIObject = Visio.Application.BuiltInMenus

    'Get the MenuSets collection.
    Set vsoMenuSets = vsoUIObject.MenuSets

    'Get the drawing window menu set.
    Set vsoMenuSet = vsoMenuSets.ItemAtID(visUIObjSetDrawing)

    'Get the Menus collection.
    Set vsoMenus = vsoMenuSet.Menus

    'Add a Demo menu.
    Set vsoMenu = vsoMenus.AddAt(1)
    vsoMenu.Caption = "Caption"

    'Get the MenuItems collection.
    Set vsoMenuItems = vsoMenu.MenuItems

    'Add a menu item to the new Demo menu.
    Set vsoMenuItem = vsoMenuItems.Add

    'Set the properties for the new menu item.

    vsoMenuItem.Caption = "Caption"
    Debug.Print ThisDocument.Name 'Shows the stencil name
    vsoMenuItem.AddOnName = "ThisDocument.modExp.TestSub" 'I have tried modExp.TestSub and just TestSub as well
    vsoMenuItem.Enabled = True

    'Tell Visio to use the new UI when the document is active.
    Visio.ActiveDocument.SetCustomMenus vsoUIObject

Sub to run:

Public Sub TestSub()
    'This sub works if I run it from the VBA editor so I won't show it here
End Sub

To clarify, the problem lies with the .AddOnName line in "GenMenu" that should call "TestSub".
  • Each sub is working independently: The menu shows in the document and TestSub can be called using "Call TestSub".
  • If the module "modExp" exists in the document instead of the stencil, everything works as it should.
  • If "GenMenu" exists in a stencil-module and "TestSub" in a document-module, everything works as it should.

Does anyone know of a solution or an easy workaround?

The stencil is meant to be loaded to a regular Visio drawing(.vsdx) and then TestSub will export certain pages.

UPDATE:
I also tried using ribbons and encountered the exact same problem. The menu shows but the sub doesn't run when I click on it.

XML called "customUI.xml":
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <ribbon>
    <tabs>
      <tab idMso="TabHome">
        <group id="customGroup" label="Export">
          <button id="exportButton" label="Export" onAction="VBA.modExp.MyButton_Click" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

Code in ThisDocument in the stencil:
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
    ' Ensure this only runs when the stencil itself is opened
    If ThisDocument.Type = visTypeStencil Then
        Call AddCustomRibbon
    End If
End Sub

Sub AddCustomRibbon()
    Dim myUI As String
    myUI = LoadCustomUIFromXML(Visio.Application.MyShapesPath & "\customUI.xml")
    Debug.Print myUI
    ' Apply the custom UI to the current document
    Visio.ActiveDocument.customUI = myUI
End Sub

Function LoadCustomUIFromXML(filePath As String) As String
    Dim fileNum As Integer
    Dim fileContent As String
   
    fileNum = FreeFile
    Open filePath For Input As fileNum
    fileContent = Input$(LOF(fileNum), fileNum)
    Close fileNum
   
    LoadCustomUIFromXML = fileContent
End Function

Finally, a generic sub to run inside module "modExp" in the stencil:
Public Sub MyButton_Click(control As IRibbonControl)
    MsgBox "Button ID: " & control.ID
End Sub


Surrogate

Quote from: hedstrompa on June 19, 2024, 09:47:08 AM    Dim vsoMenuSets As Visio.MenuSets
    Dim vsoMenuSet As Visio.MenuSet
Which Visio version do you use ?

hedstrompa

Quote from: Surrogate on June 19, 2024, 01:02:18 PM
Quote from: hedstrompa on June 19, 2024, 09:47:08 AM    Dim vsoMenuSets As Visio.MenuSets
    Dim vsoMenuSet As Visio.MenuSet
Which Visio version do you use ?

I am using Visio Plan 2:
Microsoft® Visio® Plan 2 MSO (Version 2405 Build 16.0.17628.20006) 64-bit

The drawings are also tagged with v.1.1.1

Surrogate

#3
Quote from: hedstrompa on June 20, 2024, 08:04:13 AMI am using Visio Plan 2:
WOW!
Since Visio 2010 version changed user interface from toolbars (classic) to ribbon.
Quote from: M$FT in BuiltInMenus property noteStarting with Visio 2010, the Microsoft Office Fluent user interface (UI) replaced the previous system of layered menus, toolbars, and task panes. VBA objects and members that you used to customize the user interface in previous versions of Visio are still available in Visio, but they function differently.
PS Why you need use these MenuSets and something else in 2024 ?

hedstrompa

Quote from: Surrogate on June 20, 2024, 09:06:06 AMSince Visio 2010 version changed user interface from toolbars (classic) to ribbon.
Oh thanks, I had missed that. I just figured since the code worked fine if I had it in a drawing, it should be able to run in a stencil as well.

I searched around about ribbons and it seems you would need to add code in an xml-document as well, is that correct? In my case, I want to avoid having multiple documents.

Quote from: Surrogate on June 20, 2024, 09:06:06 AMPS Why you need use these MenuSets and something else in 2024 ?
What do you mean, is there a better way to create a simple UI without plug-ins? I'm not very experienced in Visio VBA.



Surrogate

Quote from: hedstrompa on June 20, 2024, 12:21:41 PMOh thanks, I had missed that. I just figured since the code worked fine if I had it in a drawing, it should be able to run in a stencil as well.
I run code from your initial post... Where I can find this MenuItem ?

hedstrompa

Quote from: Surrogate on June 20, 2024, 01:07:17 PMI run code from your initial post... Where I can find this MenuItem ?
There should pop up a tab called "add-ins".

Surrogate


hedstrompa

Quote from: Surrogate on June 20, 2024, 04:42:44 PMI cant find this tab there  :o
If you go to File > Options > Customize Ribbon, is the "add-ins" Ribbon enabled in the right-side table? I don't know if this is something you have to enable manually.


Also, is the stencil loaded to the drawing?


Surrogate

Quote from: hedstrompa on June 24, 2024, 07:13:54 AMis the "add-ins" Ribbon enabled in the right-side table?
Oh, I find it!
Quote from: hedstrompa on June 24, 2024, 07:13:54 AMAlso, is the stencil loaded to the drawing?
IMHO you need change next row
'Tell Visio to use the new UI when the document is active.
 ThisDocument.SetCustomMenus vsoUIObject
Use ActiveDocument instead ThisDocument, because ThisDocument is a Stencil.

hedstrompa

Quote from: Surrogate on June 24, 2024, 07:41:16 AMUse ActiveDocument instead ThisDocument, because ThisDocument is a Stencil.
Oh yeah you are right, my bad. Must have copied the old sub from the drawing, the line should be
Visio.ActiveDocument.SetCustomMenus vsoUIObject

Surrogate

Quote from: hedstrompa on June 19, 2024, 09:47:08 AMUPDATE:
I also tried using ribbons and encountered the exact same problem. The menu shows but the sub doesn't run when I click on it.
For example, I want save my stencil with name Test.vss
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <ribbon>
    <tabs>
      <tab idMso="TabHome">
        <group id="customGroup" label="Export">
          <button id="exportButton" label="Export" onAction="Test!modExp.MyButton_Click" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>
Use Test!modExp.MyButton_Click instead VBA.modExp.MyButton_Click.
ThisDocument must looks like
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
    ' Ensure this only runs when the stencil itself is opened
    If ThisDocument.Type = visTypeStencil Then
        Call AddCustomRibbon
    End If
End Sub

Sub AddCustomRibbon()
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("Visio.Application.MyShapesPath & "\customUI.xml", 1)
XML = file.ReadAll
file.Close
ActiveDocument.CustomUI = XML
End Sub

hedstrompa


Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 457 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1238KB.
Tokens: post-login.
Cache hits: 14: 0.00199s for 26,724 bytes (show)
Cache misses: 4: (show)
Queries used: 20.

[Show Queries]