Custom menu does not work

C

calvinX

Hi Everyone,

I am having a problem with a custom menu that I was using in the non-MS
version of Visio 2000 that no longer works in Visio 2002 (or ms visio 2000).
The menu appears but it is greyed out. This code is located in my stencil,
but I tried moving it to "ThisDocument" and it still does not work.

Perhaps someone knows where I can get some resources on the internet or how
to fix this problem.

Thanks,

CalvinX
MY CODE:

Public Sub AddCustomMenu()
On Error Resume Next


'Attach new menu to Visio.
Dim uiObj As Visio.UIObject 'contains the UI object for
this instance of Visio
Dim MenuSetsObj As Visio.MenuSets 'MenuSets collection
Dim MenuSetObj As Visio.MenuSet 'A MenuSet object
Dim MenusObj As Visio.Menus 'Menus collection
Dim MenuObj As Visio.Menu 'A Menu object
Dim MenuItemsObj As Visio.MenuItems 'MenuItems collection
Dim MenuItemObj As Visio.MenuItem 'A MenuItem object
Dim strMenuAction As String

Set uiObj = Visio.Application.BuiltInMenus
Set MenuSetsObj = uiObj.MenuSets
Set MenuSetObj = MenuSetsObj.ItemAtID(visUIObjSetDrawing)

'Put a single menu into this set, with caption "&Special"
Set MenusObj = MenuSetObj.Menus
Set MenuObj = MenusObj.Add
MenuObj.Caption = "&Special"

Set MenuItemsObj = MenuObj.MenuItems
Set MenuItemObj = MenuItemsObj.Add
MenuItemObj.Caption = "Fill Point &List"
strMenuAction = "stencilDoc.Documentation.Point_List_Fill"
MenuItemObj.AddOnName = strMenuAction
MenuItemObj.MiniHelp = "Fill selected points list"
MenuItemObj.ActionText = "Fill selected points list"

'<<<<<<<<<SEPERATOR
Set MenuItemObj = MenuItemsObj.Add
MenuItemObj.CmdNum = 0

'<<<<<<<<<VERIFY LAYERS
Set MenuItemsObj = MenuObj.MenuItems
Set MenuItemObj = MenuItemsObj.Add
MenuItemObj.Caption = "&Verify Document Layers"
strMenuAction = "stencilDoc.Custom_Functions.Verify_Page_Layers"
MenuItemObj.AddOnName = strMenuAction
MenuItemObj.MiniHelp = "Verify correct layers are present on each page"
MenuItemObj.ActionText = "Verify correct layers are present on each
page"

'Change the current menus to the custom one
Visio.Application.SetCustomMenus uiObj

End Sub
 
M

Michael J. Hunter [MS]

Your code is correct; I tried it on Visio 2003 and it works as expected.
Visio verifies the action for each custom menu or toolbar item and disables
that menu item if the action cannot be executed. When the disabled action
specifies a VBA macro, it almost always means Visio can't find the macro to
execute. (Other reasons for disabling the item include Visio deciding it
can't run the macro due to security reasons such as VBA being disabled or
the macro not being signed when running at high security levels.)

Your action parameters explicitly specify
"stencilDoc.Documentation.Point_List_Fill", which indicates the macro is
named "Point_List_Fill" in a module named "Documentation" in a document
named "stencilDoc". If that stencil is not loaded, or it has a different
name, or the module's name changed for some reason, Visio won't be able to
find the macro and so will disable the menu item.

Note that the "stencilDoc" document's name is not the name of the file on
disk, but rather the name of its VBA project. The VBA project's name is not
automatically updated when you Save or Save As the document, so you have to
change it manually. Do this by selecting the project in the VBA IDE's
Project window, then modifying the Name property in the IDE's Properties
window.

Visio enables and disables its menus dynamically, so if your menu items are
disabled because the referenced document is not loaded, the menu items will
automatically enable as soon as you open the referenced document.
Similarly, if the problem is that the referenced document's VBA project has
the wrong name, simply modifying its name will automatically enable your
menu items.


Michael J. Hunter
Microsoft
michhu-at-online-dot-microsoft-dot-com

Developing Microsoft Visio Solutions:
http://msdn.microsoft.com/library/en-us/devref/HTML/DVS_Copyright_1270.asp
Developer Resources for Microsoft Visio:
http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?contentid=28000456

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
R

Reinier Post

calvinX said:
I am using Visio 2002 and it does not work. :(

Michael Hunter gave a number of caveats, such as

This was the stumbling block for me: it is not enough for Visio 2002 to
have the stencil open, it must actually have a code *reference* to it
for the menus to work.

Visio 2002 will only let you specify a single name in .AddOnName,
which it will then try to interpret as the name of an addon or
a Public Sub in the current project or anything it has a code reference to
(Tools->References). Even when you have a stencil that creates a document
menu with VBA code, and the stencil is open while editing a drawing,
Visio will not consider the stencil as a place to look for menu callback
macros, unless your active drawing has a code reference to the stencil
document.

I haven't found a completely satisfactory way to deal with this problem.
There are several workarounds. Using the QueuedMarkerEvent addon
mentioned in other threads has the drawback of imposing the addon as an
additional installation requirement. Other methods (using VBA to create
code, or a Scratch cell, or a code reference, in the current drawing)
have the drawback of causing the current drawing to be "modified", even
when the user didn't do anything with it.

There may be mistakes in the above but these are my experiences.
For a serious project you probably want to implement your code
as an addon anyway, which makes the problem go away.
 

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