Call macro in stencil file from a custom menu

D

drewcogswell

I have placed all my macro code into a stencil file for easier
distribution. I can successfully run the macros through the Tools >
Macros > menus, but would like to be able to run them from a custom
menu. I have created the menu using the following code, but the items
are all disabled. It seems that it does not recognize the Sub names
in the stencil module as being in the global namespace.

The visMenuItem.AddOnName = "MyMacro" statement seems to be the
problem. If I reference a macro in a Module in the .vsd Document (not
the Stencil file), it works fine.

Does anybody have an example or know how to reference the stencil file
modules?

Dim uiObj As Visio.UIObject
Dim visMenuSets As Visio.MenuSets
Dim visMenuSet As Visio.MenuSet
Dim visMenus As Visio.Menus
Dim visMenu As Visio.Menu
Dim visMenuItems As Visio.MenuItems
Dim visMenuItem As Visio.MenuItem

Set uiObj = Visio.Application.BuiltInMenus
Set visMenuSets = uiObj.MenuSets

' Get the Visio object context menu set.
Set visMenuSet = visMenuSets.ItemAtID(visUIObjSetDrawing)
Set visMenus = visMenuSet.Menus
Set visMenu = visMenuSet.Menus.AddAt(7)
visMenu.Caption = "MyMacrosMenu"

' Get the items collection from the file menu
Set visMenuItems = visMenu.MenuItems

'Get macros menu from builtin menus
Dim toolsMenu As Visio.MenuItems
Dim macroMenu As Visio.MenuItem
Dim macroSubMenu As Visio.MenuItems
Dim documentMacros As Visio.MenuItem
Dim AddOnNameText As String

'ADD ITEMS
Set visMenuItem = visMenuItems.Add
visMenuItem.Caption = "MyMacro"
visMenuItem.State = Visio.visButtonUp
visMenuItem.AddOnName = "MyMacro"
visMenuItem.Enabled = True

' Set the new menus.
Visio.Application.ActiveDocument.SetCustomMenus uiObj
 
J

JuneTheSecond

I also filed, I cannot see why.
In the past I used Office commandbar object, and worked well.
Please try.

On stencil:
Where onAction_OnStencil is the name of stencil.
Sub AddMenu1()

Dim myMenuBar As Office.CommandBar
Dim newMenu As Office.CommandBarControl
Dim ctrl1 As Office.CommandBarControl

Set myMenuBar = Application.CommandBars.ActiveMenuBar
Set newMenu = myMenuBar.Controls.Add(Type:=msoControlPopup,
Temporary:=True)
newMenu.Caption = "Menu Made by Stencil"

Set ctrl1 = newMenu.Controls.Add(Type:=msoControlButton, ID:=1)
ctrl1.Caption = "TEST"
ctrl1.OnAction = "onAction_OnStencil!ThisDocument.test"
ctrl1.TooltipText = "Menu Made by Stencil"
ctrl1.Style = msoButtonCaption

End Sub

Sub test()
MsgBox "test"
End Sub
 
J

JuneTheSecond

Now, I got conclusion.
visMenuItem.AddOnName = "MyMacro" to be edired as
visMenuItem.AddOnName = "NameOfStencil.ModuleName.MyMacro"

my test code is as follows.
Public Sub AddOnName_Example()

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

Set vsoUIObject = Visio.Application.BuiltInMenus
Set vsoMenuSets = vsoUIObject.MenuSets
Set vsoMenuSet = vsoMenuSets.ItemAtID(visUIObjSetDrawing)
Set vsoMenus = vsoMenuSet.Menus
Set vsoMenu = vsoMenus.AddAt(7)
vsoMenu.Caption = "Demo"
Set vsoMenuItems = vsoMenu.MenuItems
Set vsoMenuItem = vsoMenuItems.Add
vsoMenuItem.Caption = "Run test"
vsoMenuItem.AddOnName = "Stencil_AddonName.Module1.test"
vsoMenuItem.ActionText = "Run test"
ThisDocument.SetCustomMenus vsoUIObject

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top