Stencil File Opened Event

Started by OldSchool1948, May 30, 2018, 12:22:15 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

OldSchool1948

Is there an event that fires when a stencil file is added to a Visio file?  I've tried Document_DocumentOpened but it only detects when the Visio file is opened. 

Hey Ken



Yep, there is.  You need to add VBA to the stencil itself to handle the Document Opened event.  It fires whenever the stencil is opened.

As an aside, in general I always keep all my VBA with the stencil.  It make it much easier to perform code updates because you only need to distribute a new stencil, not change the code in innumerable drawings.

Good luck!

- Ken


Ken V. Krawchuk
Author
No Dogs on Mars - A Starship Story
http://astarshipstory.com

OldSchool1948

Quote from: Hey Ken on May 30, 2018, 12:56:04 PM


Yep, there is.  You need to add VBA to the stencil itself to handle the Document Opened event.  It fires whenever the stencil is opened.

As an aside, in general I always keep all my VBA with the stencil.  It make it much easier to perform code updates because you only need to distribute a new stencil, not change the code in innumerable drawings.

Good luck!

- Ken

I was not clear enough.   :)  I've built a design and engineering application using Visio VBA and Excel VBA.  The app uses a set of custom stencils with all custom built shapes.  I only want those shapes and stencils used. 

Each shape has a user-defined field that is checked on-drop, if the field is not present, I present an error message and delete the shape.  I want to do something similar with stencils.  That is, if an "other" stencil is attached, I want to present an error message and remove it.  I have the code that removes "other" stencils working, I just can't figure out how to call it using some "stencil attached to Visio document" event.

This app has a ton of code.  I've written a patching tool to do module and form updates.  I'll have to look deeper into placing my non-static module code in a stencil. 

wapperdude

#3
It would seem that the code must be at document level.  Two issues then.  One is potentially a major security risk.  Second, it would have to exist in the document...I think that means a template file.  There's probably other viable scenarios, e.g., template with at least one of your stencils which would contain necessary code.  Then, whenever a stencil is added, code fires, checks all stencils foe presence of valid flag, and does what's necessary.

Here's link that kinda covers it all:  https://stackoverflow.com/questions/7216653/making-a-macro-available-for-any-visio-document

Just some thoughts.

Wapperdude
Visio 2019 Pro

Hey Ken

OldSchool:

   Well, why not get heavy-handed?  Connect a little assassin routine to, say, mousedown, that closes any unauthorized stencils:



For Each vsoDocument In Visio.Documents
    If vsoDocument.Index <> ActiveWindow.Document.Index Then
        If vsoDocument.Name <> "approved name" Then
            vsoDocument.Close
            End If
        End If
    Next



   Checking .Index is important or else you'd close your main drawing!  You can make it silent or not, as you like.

   Hope this helps,

   - Ken

Ken V. Krawchuk
Author
No Dogs on Mars - A Starship Story
http://astarshipstory.com

AndyW

Your document can detect a stencil being opened, but this needs to be done at the application level. So in this example I have set the application with events when the document is opened. The application document opened event is then triggered for stencils.

Private WithEvents vsoApp As Visio.Application

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)

    Set vsoApp = ThisDocument.Application
   
End Sub

Private Sub vsoApp_DocumentOpened(ByVal doc As IVDocument)

    Debug.Print doc.Name
   
End Sub
Live life with an open mind

OldSchool1948

AndyW thank you!!  Your solution worked perfectly!!