• Want Visio macro to toggle shape angle

    Home » Forums » AskWoody support » Productivity software by function » Other MS apps » Want Visio macro to toggle shape angle

    Author
    Topic
    #468268

    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
    Viewing 5 reply threads
    Author
    Replies
    • #1219221

      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
    • #1219251

      Thanks Andrew but when I put that code in (between the sub & end sub). It does nothing that I can see.

    • #1219267

      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.

      • #1219649

        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.

    • #1219659

      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.

      • #1219681

        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.

    • #1222273

      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.

    • #1332348

      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.

    Viewing 5 reply threads
    Reply To: Want Visio macro to toggle shape angle

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: