Visio Macro

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Visio Macro

Post by dasadler »

I often will select a shape then go to the Size & Position box and change the angle from 0 to 90 or 90 to 0. This happens often enough that it seemed a good idea to have a macro to toggle the angle from one to the other. To that end I recorded a macro thinking I could look at the code and modify it to meet my needs. The code is below. What I found, though, was unlike anything I have seen in Excel or Access code and I am, once again, lost. Can anyone help? I would simply like to select an object and have its angle change from 0 to 90 or 90 to 0 when I click on the macro button. I also wasn't sure where to save the recorded macro so I selected the active sheet although I would prefer it to be available in any Visio document I have open.

Code: Select all

Sub Toggle_Shape()

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Size & Position 2-D")
    Application.ActiveWindow.Page.Shapes.ItemFromID(20).CellsSRC(visSectionObject, visRowXFormOut, visXFormAngle).FormulaU = "90 deg"
    Application.EndUndoScope UndoScopeID1, True

    Dim UndoScopeID2 As Long
    UndoScopeID2 = Application.BeginUndoScope("Size & Position 2-D")
    Application.ActiveWindow.Page.Shapes.ItemFromID(20).CellsSRC(visSectionObject, visRowXFormOut, visXFormAngle).FormulaU = "0 deg"
    Application.EndUndoScope UndoScopeID2, True

End Sub
Don

User avatar
HansV
Administrator
Posts: 78453
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Visio Macro

Post by HansV »

Eileen's Lounge is a small discussion board, we don't have many Visio experts, so you may not get an answer here - sorry about that.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Visio Macro

Post by dasadler »

Okay. I thought VBA was VBA and the logic would be to examine the object selected and if angle =0, change to 90 else change to 0. I just don't know how to write the code to do that. In the code above (from the recorded macro), I selected a rectangle, changed the angle to 90 then unselected it then selected it again and changed the angle the 0.
Don

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Visio Macro

Post by dasadler »

Also... time to actively recruit some Visio experts. Eileen's lounge may be small but it is the best IMO.
Don

User avatar
HansV
Administrator
Posts: 78453
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Visio Macro

Post by HansV »

dasadler wrote:I thought VBA was VBA.
The underlying language is the same in all applications, but the object model is different for each application. Your question is about items that are specific to Visio. Since I don't have Visio, I don't know anything about those specific items, and I cannot test your code.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78453
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Visio Macro

Post by HansV »

dasadler wrote:Also... time to actively recruit some Visio experts. Eileen's lounge may be small but it is the best IMO.
Thanks for the compliment! We can only hope that someone will answer your question eventually.
Best wishes,
Hans

William
StarLounger
Posts: 79
Joined: 08 Feb 2010, 21:48
Location: Wellington, New Zealand

Re: Visio Macro

Post by William »

The following code rotates shape one on page one 90 degrees counterclockwise:

Code: Select all

Pages.Item(1).Shapes.Item(1).Rotate90
This is probably the limit of my Visio VBA knowledge!

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Visio Macro

Post by dasadler »

Thanks William.
Don

William
StarLounger
Posts: 79
Joined: 08 Feb 2010, 21:48
Location: Wellington, New Zealand

Re: Visio Macro

Post by William »

And the following seems to work on the currently selected shape or shapes:

Code: Select all

ActiveWindow.Selection.Rotate90

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Visio Macro

Post by dasadler »

I guess I am having trouble knowing how to invoke the macro once it exists.
Don

User avatar
HansV
Administrator
Posts: 78453
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Visio Macro

Post by HansV »

Running VBA code from Visio is for Visio 2002 (XP) but I assume it's much the same for Visio 2003 and 2007 (which still has the old menu/toolbar interface; this will be replaced by the ribbon in Visio 2010).
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Visio Macro

Post by dasadler »

William wrote:And the following seems to work on the currently selected shape or shapes:

Code: Select all

ActiveWindow.Selection.Rotate90
William, this works exactly as you said it would. I am still trying to get a simple toggle between 0 & 90.

My macro is

Code: Select all

Sub Toggle_Shape()
With ActiveWindow.Selection.Item(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormAngle)
    If .Formula = "0 deg" Then
      .Formula = "90 deg"
    Else
      .Formula = "0 deg"
    End If
  End With
End Sub
If my selected shape is at 90 then it will toggle to 0. However, if the shape is 0, it will not go to 90. I reversed to code just for testing to the following

Code: Select all

Sub Toggle_Shape()
With ActiveWindow.Selection.Item(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormAngle)
    If .Formula = "90 deg" Then
      .Formula = "0 deg"
    Else
      .Formula = "90 deg"
    End If
  End With
End Sub
What I find is that the IF statement always resolves to false and the ELSE is executed. In other words, if my selected shape is at 00 then it will toggle to 90. However, if the shape is 90, it will not go to 0.
Don

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Visio Macro

Post by dasadler »

To Hans.

Thanks for the info on running the macro. Seems my problem was the macro security setting so when I changed that the macros worked fine.
Don