Search code examples
vbavisio

Getting docked Visio stencils, as opposed to the Document stencil, in VBA


I am attempting to make a Visio macro which will alphabetize the masters in the first docked stencil in a document. Below is my code. However, Set vsoDoc = Visio.Documents.ItemFromID(0) does not appear to reference the docked stencil, but the document stencil. How do I get the docked stencil? Any help much appreciated!

Dim i As Integer
Dim vsoDoc As Visio.Document
Dim vsoDocNew As Visio.Document
Dim dictMasters As New Scripting.Dictionary
Set vsoDoc = Visio.Documents.ItemFromID(0)    'Existing Stencil

Set vsoDocNew = Visio.Documents.AddEx("New Stencil.vss", , visAddStencil)     'New Sorted Stencil

'Get the names of the existing masters and sort them

For i = 1 To vsoDoc.Masters.Count
    Call dictMasters.Add(vsoDoc.Masters(i).Name, vsoDoc.Masters(i))
Next

list = dictMasters.Keys()

Dim First As Integer, Last As Long
Dim x As Long, j As Long
Dim Temp As String

First = LBound(list)
Last = UBound(list)
For x = First To Last - 1
    For j = x + 1 To Last
        If list(x) > list(j) Then
            Temp = list(j)
            list(j) = list(x)
            list(x) = Temp
        End If
    Next j
Next x

'Drop the existing masters into the new stencil based on the sorting

For i = 1 To dictMasters.Count
   Call vsoDocNew.Masters.Drop(dictMasters(list(i - 1)), 0, 0)
Next

I am hoping to produce a new stencil with the same masters as the docked stencil but alphabetized. However, the new stencil produced is from alphabetizing the document stencil. Visio.Documents.ItemFromID(0) is returning the document stencil, but I want to get the first docked stencil. How to I get the docked stencil?


Solution

  • You can loop through the ActiveWindow.Windows list and find any window whose .Type = visDockedStencilBuiltIn or you can go by the .Document.Name, and I think you can check whether the stencil is in front by checking if (.WindowState And visWSActive) = True

    So basically the way it works, the stencils aren't really attached to your document, but rather the Window in which the document is displayed. So you have to see what sub-windows are open in your document's window, where each sub-window can have an associated document (the stencil)