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

How to Call Sub in Stencil from macro/sub in document

Started by razmichael, February 26, 2009, 02:02:07 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


Hi Everyone,

I'm fairly new at Visio coding and have run into a irritating problem that I hope I can find a solution for.  I have a database that contains data to be used in part to draw diagrams in Visio.  This is generally used in a dynamic manner - make a data change, produce the diagram to review, delete the diagram and try different data. 

Initially I wrote the code in the visio document/template.  I tied the running of the code to a macro with a shortcut key to allow for quick and easy access.  To ease deployment and upgrades, I moved the code to a stencil but could no longer run this from a short cut key.  I could use the tools menu and navigate through to the stencil macros to run but this is irritating over and over again.  I have tried numerous formats to try to call a sub in the stencil but no luck.  Adding the stencil to the references solves this issue but causes other problems with paths being wrong etc.  I have looked at programing to add the ref dynamically based on the current location of the opening document but this seems like much too much work to only get to use a short cut key.  I've found the details on using CallThis and other options but none fit this problem.

Any help would be greatly appreciated.



So you want a macro in the document to run a macro in one of you stencils?
You could try the following.

In this example the following code is in my test stencil called Stencil1 (You don't need this)

Public Sub RunMe()
    MsgBox ("test")
End Sub

Then the following code would go in your document (all of it):

Sub Run()
    Dim nSencil As Integer
    Dim i As Integer
    For i = 1 To Application.Documents.Count
        If Application.Documents(i).Name = "Stencil1" Then
        nStencil = i
        End If
    Next i
    Call Application.Documents(nStencil).RunMe
End Sub

Note that it looks for the stencil called "Stencil1" you should change this to the stencils name with your macro in it.
It also calls the macro RunMe. This should be changed to your macro in the stencil.

Hope this helps, and that its clear.

- Lars


Thank you very much for the response - with a couple of modifications it solved the issue.  I added the .vss extension into the .Name = portion for the stencil name. 

Originally the sub I was trying to call in the stencil was contained in the module NewMacros and this was not being found (error 438 object does not support this property or method).  As all this sub did was open a form for user input I moved this to the ThisDocument of the stencil (The form would then run the main code in the NewMacros module when the user 'hit the button').

Out of interest, if there a way similar to your solution to call a sub in the module of the stencil?  I tried various forms of "Call Application.Documents(nStencil)...." but could not do a direct call to the module in the stencil.

Sub OpenForm()
' Keyboard Shortcut: Ctrl+l
Dim nStencil As Integer
Dim i As Integer
    For i = 1 To Application.Documents.Count
        If Application.Documents(i).Name = "PosterStencil.vss" Then
            nStencil = i
        End If
    Next i
Call Application.Documents(nStencil).OpenMainForm
End Sub


Something like this is what I suspected to work, but to my surprise it didnt.
Call Application.Documents(nStencil).Module.RunMe

A work around might be to run the macro in the module from the ThisDocument part, maybe someone else has an idea how to get this done.



It may be ExcuteLine.
Best Regards,

Junichi Yoda


Nice one June!

Call Application.Documents(nStencil).ExecuteLine("Module1.RunMe")

Does the trick.



Thanks very much to both of you.  I'm sure I would have eventually tripped over the right combination given a few more weeks of random attemps!  I will confess that I did take some satisfaction in Lar's post where you said you thought it should have worked!  That's what I have been saying for days!.  Again, thanks for the help.


Is it possible to apply a saved macro (on the hdd ) to a new visio drawing ?

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: 269 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1138KB.
Tokens: post-login.
Cache hits: 14: 0.00306s for 26,766 bytes (show)
Cache misses: 3: (show)
Queries used: 16.

[Show Queries]