Visio Guy

Visio Discussions => Programming & Code => Topic started by: OldSchool1948 on May 30, 2018, 12:22:15 PM

Title: Stencil File Opened Event
Post by: OldSchool1948 on May 30, 2018, 12:22:15 PM
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. 
Title: Re: Stencil File Opened Event
Post by: 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


Title: Re: Stencil File Opened Event
Post by: OldSchool1948 on May 30, 2018, 03:11:17 PM
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. 
Title: Re: Stencil File Opened Event
Post by: wapperdude on May 30, 2018, 06:30:53 PM
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 (https://stackoverflow.com/questions/7216653/making-a-macro-available-for-any-visio-document)

Just some thoughts.

Wapperdude
Title: Re: Stencil File Opened Event
Post by: Hey Ken on May 30, 2018, 07:26:28 PM
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

Title: Re: Stencil File Opened Event
Post by: AndyW on May 31, 2018, 08:08:39 AM
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
Title: Re: Stencil File Opened Event
Post by: OldSchool1948 on June 01, 2018, 10:17:56 AM
AndyW thank you!!  Your solution worked perfectly!!