List all Shapes in an Active Stencil (VBA)

Started by devpods, August 30, 2012, 02:29:05 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

devpods

Hi Guys,

I'm trying to have a form display a list of names for all the shapes on an Active Stencil. How do I go about the VBA for that, where Visio finds the opened/active stencil, loops through all the shapes in the particular stencil, and prints out their names?

Thanks for the help!


charlykuntz

#1
Hi,

I am not really familiar with VBA so I explain it with C# while the mechanism should be the same.

You can access the stencils using the Application.Documents property which is an indexed property.

Code (C#) Select

ActiveDocument.Application.Documents


You can iterate the Documents property with a foreach-loop

Code (C#) Select

foreach (Document doc in ActiveDocument.Application.Documents)
{
      // Do something
}


Each document has a Master property which is also iterable. Each master represents a shape in a stencil and each shape on yout drawing references on their masters.

Code (C#) Select

foreach (Document doc in ActiveDocument.Application.Documents)
{
      foreach (Master mst in doc.Masters)
      {
             MessageBox.Show(mst.Name)
      }
}

Jumpy

You could add a check, to see, if doc is a stencil:

If Instr(UCase(doc.Name),".vss")>0 then...

aledlund

You might try something like this
al




Option Explicit


Const visWSActive = &H4000000       'Active window.
Const visWSAnchorAutoHide = &H200   'Anchor window with AutoHide on.
Const visWSAnchorBottom = &H100     'Window is anchored at the bottom.
Const visWSAnchorLeft = &H20        'Window is anchored at the left.
Const visWSAnchorMerged = &H400     'Window is merged.
Const visWSAnchorRight = &H80       'Window is anchored at the right.
Const visWSAnchorTop = &H40         'Window is anchored at the top.
Const visWSDockedBottom = &H8       'Window is docked at the bottom.
Const visWSDockedLeft = &H1         'Window is docked at the left.
Const visWSDockedRight = &H4        'Window is docked at the right.
Const visWSDockedTop = &H2          'Window is docked at the top.
Const visWSFloating = &H10          'Window is floating.
Const visWSMaximized = &H40000000   'Window is maximized.
Const visWSMinimized = &H20000000   'Window is minimized.
Const visWSNone = &H0               'No window state.
Const visWSRestored = &H10000000    'Window is restored.
Const visWSVisible = &H8000000      'Window is visible.


Public Sub readActiveWindowMasters()
   
    Dim visWin As Visio.Window
    Dim visRootWin As Visio.Window
    Dim visRootWins As Visio.Windows
    Dim visWins As Visio.Windows
    Set visWins = Application.Windows
   
    Dim intWins As Integer
    Dim intX As Integer
   
    For Each visRootWin In visWins
        Set visRootWins = visRootWin.Windows
        intWins = visRootWins.Count
        If intWins > 0 Then
            For intX = 1 To visRootWins.Count
                Set visWin = visRootWin.Windows(intX)
                Dim intBin As Long
                intBin = visWin.WindowState And visWSActive
                If 0 < intBin Then
                    ' the stencil Window Name
                    Debug.Print visWin.Caption

                    Dim visDoc As Visio.Document
                    Set visDoc = visWin.Document
                    Dim visMaster As Visio.Master
                    For Each visMaster In visDoc.Masters
                        ' now the master names
                        Debug.Print visMaster.NameU

                    Next visMaster
                End If
            Next intX
        End If
    Next visRootWin
   
   
End Sub



metuemre

Thanks to code posted by aledlund, I can get the active stencil but if I want to identify the selected master in the active stencil what should I do?

I want to add a custom context menu to each master but the text of the context menu will be different based on the name of the Master. It is easy to identify the shapes in the drawing window with ActiveWindow.Selection.PrimaryItem but I couldn't find a way to refer to the selected master in the Active Stencil.

Any suggestion is appreciated.

Regards

metuemre