I have an application developed in a Visio Stencil.
I want to capture a Page Rename.
When within a Document the Document_PageChanged event works nicely, however this does not work when in the Stencil.
Any way to capture this from within the Stencil?
You need to attach a handler to the "page changed" event of the target document instead of the stencil document.
I would try to approach the problem with code something like this:
Dim WithEvents mainDocument As Document
Private Sub mainDocument_PageChanged(ByVal Page As IVPage)
MsgBox "PageChanged"
End Sub
Private Sub mainDocument_BeforeDocumentClose(ByVal doc As IVDocument)
Set mainDocument = Nothing
End Sub
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
Set mainDocument = FindTargetDocument(doc)
End Sub
' find the target document this stencil is attached to
'
Private Function FindTargetDocument(ByVal doc As IVDocument)
' loop through all windows
'
Dim w As Window
For Each w In Application.Windows
' get all stencils names for the window
'
Dim dockedStencilNames() As String
w.DockedStencils dockedStencilNames
' loop through stencil names
'
For idx = LBound(dockedStencilNames) To UBound(dockedStencilNames)
dockedStencilName = dockedStencilNames(i)
' if our stencil is the docked one we have found the target doc
'
If dockedStencilName = doc.FullName Then
Set FindTargetDocument = w.Document
End If
Next
Next
End Function
The above code will not work in certain edge cases of course and you may need to improve it if you want to support those.
For example, if you open a stencil not as a docked one, but as standalone file.
Or it may need some rework to support multiple target files.
It is just to illustrate the idea.