JleruOHeP
JleruOHeP

Reputation: 10386

Connect two 2-d shapes in visio

I am trying to do the simplest thing - connect two 2d shapes in visio using macro with GlueTo. I have dropped two shapes, added to them connection points with "In" and "Out". Now when I am trying to connect them with a mouse - it`s all ok. Then I have recorded a macro:

Sub Macro1()

'Enable diagram services
Dim DiagramServices As Integer
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140

Dim UndoScopeID1 As Long
UndoScopeID1 = Application.BeginUndoScope("1")
ActiveWindow.DeselectAll
ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(1), visSelect
Application.ActiveWindow.Selection.Move -1.161417, 0.669291
Dim vsoCell1 As Visio.Cell
Dim vsoCell2 As Visio.Cell
Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsU("PinX")
Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(15).CellsSRC(7, 0, 0)
vsoCell1.GlueTo vsoCell2
Dim vsoCell3 As Cell
Dim vsoCell4 As Cell
Set vsoCell3 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionConnectionPts, 0, 0)
Set vsoCell4 = Application.ActiveWindow.Page.Shapes.ItemFromID(15).CellsSRC(visSectionConnectionPts, 0, 0)
vsoCell3.GlueTo vsoCell4
Application.EndUndoScope UndoScopeID1, True

'Restore diagram services
ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

And it is not working... If I unglue those shapes and run recorded macro, it will throw "Inappropriate source object for this action"

How can I fix it?

Upvotes: 1

Views: 4363

Answers (1)

user9182
user9182

Reputation:

The recorded code is very specific to the state of the drawing when you recorded the macro. It will break very quickly as you edit the drawing. For example, the recorded code assumes there is already a 1-D shape on the page and that has an ID of 15 in the page's shapes collection. There are lots of other very brittle assumptions in the recorded macro.

I suggest a better approach is to learn how to code the macro yourself since you know the functional requirements and the assumptions you can make about the state of the drawing. See this section of the Developing Visio Solutions book to learn how to write code to connect shapes together: Creating a Connected Drawing from a Program.

Upvotes: 2

Related Questions