Search code examples
vbams-officevisio

MS Visio Drop a custom shape using VBA


I can't seem to figure out how to drop a shape using VBA.

What I want to do is: The user opens a UserForm and enters something in the TextBoxes. When clicking on the commandbutton I want to load a Shape (i.e. ressource) from a custom stencil (i.e. shapes.vssx) write the User-Entries into the ShapeData (i.e. write a Name string in Props.Name) and then Drop it somewhere on the sheet. I know I have to use the Shape.Drop method but how do I reference the specific Master-Shape I want to use for creating the new shape?

So far I am trying with this

Private Sub CommandButton1_Click()
   Dim shp As Visio.Shape
   Dim page As Visio.page
   Set page = Application.ActiveWindow.page
   Set shp = Application.Documents.Item("shapes.vssx").Masters.ItemU("ressource")

   page.Drop shp, 1, 1
End Sub

Which returns a type mismatch. What am I missing?


Solution

  • You're looking to drop a Master rather than a Shape so try this modification of your code (untested):

    Private Sub CommandButton1_Click()
       Dim mst as Visio.Master
       Dim shp As Visio.Shape
       Dim pag As Visio.page
       Set pag = Application.ActiveWindow.Page
       Set mst = Application.Documents.Item("shapes.vssx").Masters.ItemU("ressource")
       'You might also want to add some checks that the target document and then master exist
       Set shp = pag.Drop(mst, 1, 1)
    End Sub