Site icon bVisual

Re-opening Default Stencils

A recent newsgroup question asked if it is possible to re-open the default stencils that were originally docked when the drawing was created.    At first, this may seem impossible, but, if the original template is present, then it can be silently opened, the docked stencils noted, then opened docked if they are not already docked.  Here’s how (once you have created a reference to the Microsoft Scripting Runtime):

Public Sub OpenDefaultStencils()

Dim srcDoc As Visio.Document
    Set srcDoc = Visio.ActiveDocument
Dim tmplt As String
    tmplt = srcDoc.Template
    If Len(tmplt) = 0 Then
        Exit Sub
    End If

Dim fs As FileSystemObject
    Set fs = New FileSystemObject
    If fs.FileExists(tmplt) = False Then
        Exit Sub
    End If
Dim doc As Visio.Document
    Set doc = Visio.Application.Documents.OpenEx(tmplt, Visio.visOpenCopy + Visio.visOpenHidden + Visio.visOpenDontList)

Dim aryStens() As String
Dim win As Visio.Window
Dim arySrcStens() As String
Dim winSrc As Visio.Window
    For Each win In Visio.Windows
        If win.Document Is doc Then
            win.DockedStencils aryStens
        ElseIf win.Document Is srcDoc Then
            Set winSrc = win
            winSrc.DockedStencils arySrcStens
        End If
    Next
    ‘Close the template document
    doc.Close

Dim sten As String
Dim i As Integer
    For i = 0 To UBound(aryStens)
        If aryContains(arySrcStens, aryStens(i)) = False Then
            Visio.Application.Documents.OpenEx aryStens(i), Visio.visOpenRO + Visio.visOpenDocked
        End If
    Next
End Sub

Private Function aryContains(ByVal objSten As Variant, ByVal sten As String) As Boolean
Dim aryStens() As String
    aryStens = objSten
Dim i As Integer
    For i = 0 To UBound(aryStens)
        If aryStens(i) = sten Then
            aryContains = True
            Exit Function
        End If
    Next i
    aryContains = False
End Function

Exit mobile version