如何在 Visio 中突出显示组中的形状 VBA

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

我想突出显示与特定组对应的形状。以下代码仅突出显示与活动页面或母版分组但不与活动页面中存在的组分组的形状。

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

我认为您想以编程方式 select 组内的子形状。在 Visio 中执行此操作并不明显,所以请让我帮忙。我将在 my website 上放置两篇文章的链接,在 post 末尾放置一篇关于 Microsoft 的文章。它们更详细地讨论了 select 与离子相关的主题。

所以让我们来解决您的问题...

设置

  1. 在 Visio 中打开空白绘图
  2. 绘制两个矩形,然后将它们分组

你现在在这个页面上有 三个 个形状。

  • Sheet.1 是子形状
  • Sheet.2 是子形状
  • Sheet.3是组

您可以像这样以编程方式select群组,正如您发现的那样:

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

顺便说一下,上面的 Sub 比它必须的要挑剔和长得多。但是,当您开始添加功能和行为时,让事情简单明了会有所帮助。实际上,您可以像这样将整个过程连成一行——您甚至可以将其粘贴到立即 window:

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

现在子select Sheet.1 或 Sheet.2。有人会认为我们可以简单地将 shp 对象更改为子形状之一,ala:

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

但这行不通。事实上你会得到一个 "Inappropriate target object for this action" 错误。

要解决这个问题,我们必须向 Select 方法传递一个不同的参数:

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

瞧!在活动 window!

中编辑子形状 select

如果您想检测哪些形状已经 selected,那么您必须 fiddle 使用 IterationMode 属性 Select离子物体。这很令人困惑,而且我认为您现在不会要求这样做。但了解该术语将有助于您在将来需要时寻求帮助。

文章

Getting a Handle on Selecting and Subselecting Visio Shapes

Detect Sub-selected Shapes Programmatically

Selection.Select method (Visio)