Vikas Arya
Vikas Arya

Reputation: 5

How to highlight a shape present in a group in Visio VBA

I want to highlight the shape corresponding to a particular group. The following code is only highlighting shapes that are grouped with active page or master but not with the group present in the active page.

Sub CA_Trace_Conflict1()
    PCC_CA = InputBox("Enter PCC Band")

    'SCC1_CA = InputBox("Enter SCC1 Band")
    Dim shp As Visio.Shape
    Dim subshp As Visio.Shape
    Dim connectorshape As Visio.Shape
    Dim BandLinewidth As String
    Dim lngShapeIDs() As Long
    Dim count As Integer
    Dim PCC_Flag As Integer
    Dim SCC1_Flag As Integer

    PCC_Flag = 0
    SCC1_Flag = 0

    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150
    Dim UndoScopeID1 As Long

    PCC_CA_space = PCC_CA & " "
    For Each shp In Visio.ActivePage.shapes
        If shp.Type = 2 Then 'Check if shp is a group
         For Each subshp In shp.shapes
                If InStr(shp.Text, PCC_CA_space) > 0 Then
                'If PCC_CA Like shp.Text Then
                Set connectorshape = shp
                Debug.Print shp.Parent
          Application.ActiveWindow.Page.shapes.ItemFromID(shp.ID).CellsSRC(visSectionObject,visRowLine, visLineWeight).FormulaU = "5.5 pt"
               '   Debug.Print shp.ID
                End If
            Next
     End If
    Next

End Sub

Upvotes: 0

Views: 868

Answers (1)

Visio Guy
Visio Guy

Reputation: 211

I think you want to select a subshape within a group programmatically. Doing this in Visio is not obvious, so let me help. I'll put links to two articles on my website, plus one on Microsoft's at the end of the post. These discuss selection-related topics in further detail.

So let's tackle your problem...

Setup

  1. Open a blank drawing in Visio
  2. Draw two rectangles, then group them

You now have three shapes on this page.

  • Sheet.1 is a subshape
  • Sheet.2 is a subshape
  • Sheet.3 is the group

You can programmatically select the group like this, as you've discovered:

Public Sub SelectGroup()

  '// Get the active window:
  Dim win As Visio.Window
  Set win = Visio.ActiveWindow

  '// Deselect everything:
  Call win.DeselectAll

  '// Get a shape object:
  Dim shp As Visio.Shape
  Set shp = Visio.ActivePage.Shapes.ItemFromID(3) '<<----- Sheet.3 is the group!

  '// Cause that shape to be selected in the window:
  Call win.Select(shp, Visio.VisSelectArgs.visSelect)

  '// Cleanup:
  Set shp = Nothing
  Set win = Nothing

End Sub

By the way, the Sub above is much more nitpicky and long than it has to be. But it will help to have things simple and clean, when you start adding features and behaviors. You can actually one-line the whole procedure like this--you can even paste this into the Immediate window:

Call Visio.ActiveWindow.Select(Visio.ActivePage.Shapes.ItemFromID(3), Visio.VisSelectArgs.visDeselectAll + Visio.VisSelectArgs.visSelect)

Now to subselect Sheet.1 or Sheet.2. One would think we could simply change the shp object to be one of the subshapes, ala:

'// Sheet.1 is a subshape, you'll get an error
Set shp = Visio.ActivePage.Shapes.ItemFromID(1)  '<<----- ID = 1

but this won't work. In fact you'll get an "Inappropriate target object for this action" error.

To fix this, we have to pass a different argument to the Select method:

Public Sub SelectSubshape()

  '// We've drawn two rectangles on a blank page, then
  '// grouped them. Sheet.1 and Sheet.2 are subshapes,
  '// Sheet.3 is the group.

  '// Get the active window:
  Dim win As Visio.Window
  Set win = Visio.ActiveWindow

  '// Deselect everything:
  Call win.DeselectAll

  '// Get a subshape object:
  Dim shp As Visio.Shape
  Set shp = Visio.ActivePage.Shapes.ItemFromID(2)

  '// Cause that shape to be SUBSELECTED in the window.
  '// Note the different argument: visSubSelect
  Call win.Select(shp, Visio.VisSelectArgs.visSubSelect) ' <<------ visSubSelect!

  '// Cleanup:
  Set shp = Nothing
  Set win = Nothing

End Sub

Voila! Subshape selected in the active window!

If you want to detect which shapes are already selected, then you'll have to fiddle with the IterationMode property of a Selection object. This is pretty confusing, plus I don't think you're asking for that right now. But knowing the term will help you search for help in the future, should you need it.

Articles

Getting a Handle on Selecting and Subselecting Visio Shapes

Detect Sub-selected Shapes Programmatically

Selection.Select method (Visio)

Upvotes: 1

Related Questions