0

I have the following code:

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

Whenever a shape is dropped onto the screen, a user form is presented to the user. When it is submitted, this code runs.

Currently, I can only output the ID of the shape first dragged onto the screen shown by this line:

Set shp1 = Application.ActivePage.Shapes(1)

How can I change this so that the ID of the shape dragged onto the screen is shown instead?

Thank you

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Kamran64
  • 23
  • 4
  • How do you show the form? Is it shown from some event handler? – Nikolay Jun 30 '18 at 11:05
  • Yes, an event drop handler in the shape sheet runs the code to open the form. – Kamran64 Jun 30 '18 at 11:17
  • Possible duplicate of [Finding id of shape dragged onto screen in Visio using VBA macro](https://stackoverflow.com/questions/51061741/finding-id-of-shape-dragged-onto-screen-in-visio-using-vba-macro) – Nikolay Jul 02 '18 at 15:01

1 Answers1

2

If you are using EventDrop handler and a ShapeSheet cell, you can just pass shape ID to your function. You could use something like the formula below (where ID() is a built-in function returning shape ID. You can use it to get the shape from the OnDrop handler defined in your VBA code. "&" is used to concatenate text strings in VBA:

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

And then in VBA:

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

Better, you can use CALLTHIS instead of RUNMACRO (it always passes subject shape as a first parameter)

CALLTHIS("ThisDocument.OnDrop")

And then in VBA:

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

In the event handler, before you show the form, you need to remember the shape, and then you can pass it to the form.

Please note that I assume in the above examples that "OnDrop" is defined in "ThisDocument". If it's defined in a module, you don't need "ThisDocument." prefix

Yet another option could be to handle the "Shape Added" event in the VBA instead of specifying the ShapeSheet formula. Your event handler receives shape being dropped as a parameter in this case.

Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • Thanks! This helps a ton. – Kamran64 Jul 01 '18 at 12:19
  • If this answers your question, mark it as an answer. – Nikolay Jul 01 '18 at 17:00
  • It helps but I still have yet to implement this as a result of submitting the user form rather than as soon as the shape is dropped. In other words, how can I use the built in ID() function in other subroutines? – Kamran64 Jul 01 '18 at 17:18
  • You can use ID() function in the ShapeSheet, it seems it has nothing to do with subroutines. – Nikolay Jul 01 '18 at 19:07
  • Ah ok I see. Is there any equivalent function or method to get the ID via vba? – Kamran64 Jul 01 '18 at 20:14
  • Yes but my other problem is that I can't enter parameters into the subroutine that detects that the user form has been submitted. – Kamran64 Jul 01 '18 at 21:55
  • Well maybe you don't need to. You can add a property (i.e a field, a variable) to your form, and set it to the shape ID before showing the form. Then in the from, in the click event handler, you can just use that variable. I would check how to pass parameters to forms (or other objects), looks it is more of vba than of visio. – Nikolay Jul 02 '18 at 05:35
  • Okay, I will have to take a look and see if I can figure that out. For now, I will mark this question as resolved since technically you did answer it. If I run into any further problems, I'll write a new question. Thanks again! – Kamran64 Jul 02 '18 at 10:51
  • 1
    I think this is the same question / answer as over here: https://stackoverflow.com/questions/51061741/finding-id-of-shape-dragged-onto-screen-in-visio-using-vba-macro Why did it get asked twice? – JohnGoldsmith Jul 02 '18 at 14:32
  • Correct - it seems to be a duplicate. Marked as such. – Nikolay Jul 02 '18 at 14:59