Results 1 to 9 of 9
  1. #1
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Want Visio macro to toggle shape angle

    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 which I would put on a toolbar.

    Not being much of a code guy, I expect 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 below (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. I also wasn't sure where to save the recorded macro so I selected the active sheet although I would want it to be available in any Visio document I have open since I use it a lot for room layouts of furniture, machinery & equipment.


    Code:
    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

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,467
    Thanks
    3
    Thanked 130 Times in 123 Posts
    If you are doing room layouts then I would try for a macro that goes in all four compass points rather than 0 or 90. This can be done with a one liner
    Code:
    ActiveWindow.Selection.Rotate90
    The larger toggle option you described would work something like this
    Code:
    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
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Andrew but when I put that code in (between the sub & end sub). It does nothing that I can see.

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,467
    Thanks
    3
    Thanked 130 Times in 123 Posts
    Did you select an object on the page before running the code? Your sample code included a specific object to rotate, my effort allowed you to select an object to run the code on.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #5
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Andrew Lockton View Post
    Did you select an object on the page before running the code? Your sample code included a specific object to rotate, my effort allowed you to select an object to run the code on.
    My problem with the macro not running was due to the macro security setting within Visio. Once I changed that, the macro runs but not as expected.

    The macro is as follows:
    Code:
     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:
        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 0 then it will toggle to 90. However, if the shape is 90, it will not go to 0.

  7. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,467
    Thanks
    3
    Thanked 130 Times in 123 Posts
    Try a debug line to see what the value of .formula is. In front of the If statement add this line
    Debug.print .Formula

    Then show your immediate window (Ctrl-G) and run the code. What is the value shown in the immediate window?

    Have you also tried the alternate one line code I offered? I thought this would be better for floor layouts since it gives you four directions rather than two.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #7
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Andrew Lockton View Post
    Try a debug line to see what the value of .formula is. In front of the If statement add this line
    Debug.print .Formula

    Then show your immediate window (Ctrl-G) and run the code. What is the value shown in the immediate window?

    Have you also tried the alternate one line code I offered? I thought this would be better for floor layouts since it gives you four directions rather than two.

    1) The one line of code works well and I have it as a rotate macro... the other is just a fast toggle when I work with rectangular objects (shapes).

    2) I did the debug thing as you suggested and found that the value of .formula was '0 deg.' NOTE the period at the end of deg. So, I changed the macro to include a period after every instance of deg and it works fine now.

    3) Many thanks to you for this. Now, if I can figure out how to have a button on the tool bar to which I can assign the macro. I can only get one button that shows a dropdown with all the macros.

  9. #8
    New Lounger
    Join Date
    May 2010
    Location
    Seatte, WA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perhaps I'm missing something here, but Visio has a tool for rotating 90 degrees clockwise and another for counter clockwise. It's in the Actions toolbar that you can activate by right clicking in the toolbar area. Unless there's a requirement I'm not inferring from the description, writing code for this is like killing a fly with a hammer. With the Actions toolbar you get the feature and the tool icons.

  10. #9
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure why I never brought closure to this issue. brettnewman is absolutely correct and I must have been unaware of the a action toolbar.

    Thanks to all for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •