在 Visio 中拖到屏幕上的形状的输出 ID-VBA

Output ID of the shape dragged onto the screen in Visio-VBA

我有以下代码:

Sub CommandButton1_Click()
   Dim NoIO As String
   Dim shp1 As Visio.Shape
   Dim i As Integer

   Set shp1 = Application.ActivePage.Shapes(1)
   NoIO = ComboBox1.Value

   If NoIO = "7" Then
      MsgBox shp1.id
      'Target shape id selected'
      'Change shape data of that shape'
   End If
   Unload Me
End Sub

只要将一个形状放到屏幕上,就会向用户显示一个用户表单。提交后,此代码运行。

目前,我只能输出第一个拖到屏幕上的形状的 ID,如下所示:

Set shp1 = Application.ActivePage.Shapes(1)

如何更改此设置,以便显示拖到屏幕上的形状的 ID?

谢谢

如果您使用的是 EventDrop 处理程序和 ShapeSheet 单元格,则只需将形状 ID 传递给您的函数即可。您可以使用类似下面的公式(其中 ID() 是返回形状 ID 的内置函数。您可以使用它从 VBA 代码中定义的 OnDrop 处理程序中获取形状. "&" 用于连接 VBA:

中的文本字符串

RUNMACRO("ThisDocument.OnDrop("& ID() &")")

然后在VBA:

Sub OnDrop(shapeId)
  Debug.Print shapeId
  Set shape = ActiveDocument.Shapes.ItemFromID(shapeId)
   ' do something with the shape
End Sub

更好的是,您可以使用 CALLTHIS 而不是 RUNMACRO(它总是将主题形状作为第一个参数传递)

CALLTHIS("ThisDocument.OnDrop")

然后在VBA:

Sub OnDrop(x As Shape)
   Debug.Print shape.ID
   ' do something with the shape
End Sub

在事件处理程序中,在显示表单之前,您需要记住形状,然后才能将其传递给表单。

请注意,我在上面的示例中假设 "OnDrop" 是在 "ThisDocument" 中定义的。如果它是在模块中定义的,则不需要 "ThisDocument." prefix

另一种选择是处理 VBA 中的 "Shape Added" 事件,而不是指定 ShapeSheet 公式。在这种情况下,您的事件处理程序接收被丢弃的形状作为参数。