Menu Macro in Stencil's VBA Project

Started by perry59, January 11, 2015, 07:11:03 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

perry59

I grabbed some code to create a menu for some macro's (should look familiar, got it here! see below)
I want to have it in the stencil that contains the macro's. I put it in the stencil and it executes just fine, creates the menu, but the menu selections are all greyed out. Apparently the menu cannot find the macro's it references regardless code their names (macro name, module name.macro name, project name. module name.macro name)
after a little googling I found that you need to set a reference to the stencil in tools-references. This will fix the problem, but creates another. The reference has to be set in the "document", it cant be set in the stencil.
This makes it rather useless to have the macro menu code in the stencil because each time you create a new drawing and attach the stencil you have to go into the VBA editor and set a reference to the stencil.
I would like to have this menu available to anyone who uses the stencil without having to set references. Is there some way to accomplish this?
thanks

---menu code---
Public Sub Add_WiringMenu()

    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

    'Dim visAccelTblsObj As Visio.AccelTables
    Dim visAccelTblObj As Visio.AccelTable
    Dim visAccelItemsObj As Visio.AccelItems
    Dim visAccelItem As Visio.AccelItem

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

    ' Get accelarator tables
    'Set visAccelTblsObj = uiObj.AccelTables
   
    ' Get the Visio object context menu set.
    Set visMenuSet = visMenuSets.ItemAtID(visUIObjSetDrawing)
    Set visMenus = visMenuSet.Menus
    Set visMenu = visMenuSet.Menus.AddAt(7)
    visMenu.Caption = "Wiring"
   
    ' 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
   
    Set toolsMenu = visMenuSet.Menus.Item(5).MenuItems
    Set macroMenu = toolsMenu.Item(12)
    Set macroSubMenu = macroMenu.MenuItems
     
    ' Add Example Menu Item to the File menu in the 4th position
    Set visMenuItem = visMenuItems.Add
    visMenuItem.Caption = "Create wires"
    visMenuItem.State = Visio.visButtonUp
    'visMenuItem.AddOnName = "testmenu"
    visMenuItem.AddOnName = "Wiring.Wires.testmenu"

    Set visMenuItem = visMenuItems.Add
    visMenuItem.Caption = "Wire visibility"
    visMenuItem.State = Visio.visButtonUp
    visMenuItem.AddOnName = "CopyPageToDoc"
   
    Set visMenuItem = visMenuItems.Add
    'visMenuItem.BeginGroup = True
    visMenuItem.Caption = "List Wires"
    visMenuItem.State = Visio.visButtonUp
    visMenuItem.AddOnName = "FitToPageAll"

    ' Set the new menus.
    Visio.Application.SetCustomMenus uiObj
   
    ' Tell Visio to use the new UI when the document is active.
    ThisDocument.SetCustomMenus uiObj
   
End Sub
what, me worry?

JuneTheSecond

#1
I am not in front of Visio, but please try to change last line into ActiveDocument.
In this case ThisDocument means the stencil.
Best Regards,

Junichi Yoda
http://june.minibird.jp/

perry59

#2
thank you Junichi
I was somewhat suspicious of that line, but changing it to "activedocument" made no difference.
the macro menu DOES appear in the active document even with that last line of code specifying "thisdocument".
its just that the menu selections are all disabled, it does not see the code in the stencil without setting a reference to the stencil in the VBA editors tools->references. that's the problem!
Seems that visio is making this nearly impossible to do without creating a full blown addon.
perry
what, me worry?

JuneTheSecond

#3
Now I am back and examined my old file.
You need to note some more points as described in the document below.

http://msdn.microsoft.com/en-us//library/windows/apps/xaml/ms425788.aspx
To call a procedure in a project other than the project of the active document, use the syntax projName.modName.procName (you must have explicitly set a reference to projName in your Visual Basic project).

Ive attached a picture indicating macro and VBA project reference from Visio drawing to Visio stencil.
Best Regards,

Junichi Yoda
http://june.minibird.jp/

JuneTheSecond

By the way how would you like ribbon.
It looks here is an excellent document for us to run macro in stencil with ribbon.
http://blog.bvisual.net/2011/03/14/how-to-run-vba-macros-from-a-ribbon-button-in-visio-2010/
Best Regards,

Junichi Yoda
http://june.minibird.jp/

perry59

There seems to be some misunderstanding here.
All of my macro code, and the macro menu is contained in a stencil. Whenever a drawing is opened or created and attaches that stencil I would like the document to display the macro menu in order to call macro's in the stencil.
The macro menu DOES appear in the active document. However its menu items are greyed out. If I set a reference in the document to the stencil, then the macro menu "see's" the macros just fine.
In the macro menu, it makes no difference whether the addon statement is coded as projName.modName.procName or modName.procName or just procName. The macros are not seen by the menu until I set a reference in the active document.

I don't want to have to ask my users to go into the vba editor and set a reference to the stencil each time they create a new drawing and use that stencil, but apparently that must be done.
I don't know if the ribbon menu is any different, we are still on visio 2007 here.

thanks
Perry


what, me worry?

JuneTheSecond

#6
Another idea is Office.Commandbar.
But Visio drawing also needs to have reference to the stencil to run macro on stencil.

In any way the reference does not be lost, if the Visio drawing has a dummy macro.
If the Visio drawing that has stencil and the reference to stencil is saved as a form of template.
User can can make a copy from the template by double clicking the template, and get a new drawing with the stencil and the reference does not disappear.

And another easy way is to make right-click menu by adding action section on the master shape in the stencil.

Best Regards,

Junichi Yoda
http://june.minibird.jp/

perry59

what, me worry?

Yacine

#8
Kon'nichiwa Junichi-san,
We were confronted with the very same problem some years ago and opted for a solution stencil + template, but reading this post I wanted to try the pure "stencil" solution.
It works ... but: the document_opened event of the stencil does not fire every time. Specially not if there is already another drawing opened with the same stencil.
Do you happen to know how to overcome this problem?
Regards,
Yacine
Yacine

perry59

Yacine,
Do you have a menu in your stencil which successfully calls code from your stencil without having to set a reference to the stencil within your active document?
what, me worry?

Yacine

#10
In the new solution without template I do set the reference by code. The routine is called by the document opened event.
Sub AddReference(ByVal doc As IVDocument)
    Dim VBAEditor As VBIDE.Vbe
    Dim vbProj As VBIDE.VBProject
    Dim chkRef As VBIDE.Reference
    Dim BoolExists As Boolean

    Set VBAEditor = Application.Vbe
    Set vbProj = ActiveDocument.VBProject

    For Each chkRef In vbProj.References
        If chkRef.Name = "Menus" Then
            BoolExists = True
            GoTo CleanUp
        End If
    Next

    Debug.Print doc.Path & doc.Name
    vbProj.References.AddFromFile doc.Path & doc.Name
   
CleanUp:
    If BoolExists = True Then
        Debug.Print "Reference already exists"
    Else
        Debug.Print "Reference Added Successfully"
    End If

    Set vbProj = Nothing
    Set VBAEditor = Nothing
End Sub



In my original stencil + template solution I have one small routine in the ThisDocument of the drawing.

Public Function start(callFunction As String)
    CallByName Application.Documents("NameOfYourStencil.vss"), callFunction, VbMethod
End Function

All the menu items are however in the stencil.
The only restriction is the need to start from the right template.
Yacine

perry59

Yacine, you ARE a hero member!
Thanks so much, that is exactly what I was looking for! you made my day ;D
Now I just need to beef up my menu code so I wont get multiple menus and to remove the menu if the corresponding stencil is closed.

Works great though !!

perry

PS.
curious though, in your other snippet for your previous stencil + template solution. Why would you even need to run that?
why not just insert the menu in your template drawing?
what, me worry?

Yacine

#12
The routine in the templates makes the reference to the stencil unnecessary.
I enclose a basic stencil and template that implements such a menu.
Yacine

perry59

what, me worry?

JuneTheSecond

I also thank you, Yacine.
Then I tryed ot make a stencil "Menu 2.vss" using Yacine's macro to add reference.
This stencil can be open from a Visio drawing without macro.
When opened on the drawing, menu is made and the reference is added.
The stencil is attached below.
Please enjoy.

By the way: The type VBIDE, VBE, VBProject are not supported in Visio 2013, because Microsoft support for old Office 2003 is already terminated.
Then I changed them into "as Object" in Yacine's macro "addreference" in order to enable the macro in Visio 2013.

Thank you again for your help, Yacine.

Best Regards,

Junichi Yoda
http://june.minibird.jp/