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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Chris Webb's BI Blog

Microsoft Analysis Services, MDX, DAX, Power Pivot, Power Query and Power BI

davecra.wordpress.com/

Microsoft Office 365 Development, and more...

PowerShell.Amsterdam

Automate, Accelerate, Accurate

johnvisiomvp

Life with Visio and other Microsoft Toys!

Title (Required)

Windows Server Essentials Tips & Tricks

Nilsandrey's Weblog

Just another WordPress.com weblog

Things that Should be Easy

Every so often (too often in the IT industry) I encounter things that should have been very easy to do but turned out to be far too complicated. My favorite topics include SharePoint, .Net development, and software architecture, especially distributed systems.

Visio Guy

Shapes, Stencils, Drawings Templates, Tutorials, Tips & Developer Info for Microsoft Visio

Hannes's Virtual Earth

Tips & Tricks around Mapping and Cloud Computing

Pluralsight blog

be smart, be clear, be visual ...

%d bloggers like this: