none
How do I write VBA script code to select toggle menu items only if the desired condition does not currently exist? RRS feed

  • Question

  • I am writing a VBA script in which TestPartner (via automation) is to select the View menu (from the menu bar) using {Alt v} and then select a View menu item IF it is not enabled.  In other words, if the item does not have a checkmark next to it, the item is disabled, and TestPartner needs to select this menu item in order to enable it.

    The reverse is true for menu items that should be disabled, but if they are enabled (they have the checkmark displayed next to them), then TestPartner needs to select those menu items.

    How do we check to see if we select an item to change the state of that item (enabled->disabled, disabled->enabled)?

    Thanks.


    John J. Bottiger (QA Engineer, Gladiator Innovations LLC)

    Tuesday, July 3, 2012 6:19 PM

All replies

  • How do we check to see if we select an item to change the state of that item (enabled->disabled, disabled->enabled)?

    I hope we are on the same wave length here and what you want to do is toggle the state of an object between Enabled and Disabled? Is this correct. If so, the following will change the state of an object to Not its current state which effectively toggles it to the opposite state to which it is in.

    The fact that I used a CheckBox is irrelevant; you can do this with any Object Property that has a True or False state.

    Me.CheckBox1.Enabled = Not Me.CheckBox1.Enabled

    If not what you are after then need some further clarification.


    Regards, OssieMac

    Wednesday, July 4, 2012 5:55 AM
  • OssieMac,

    This is basically what I want to do:  I want to change the state of an object only if it's current state is not where I want it to be.

    So the logical flow is as follows:

    For each view menu item

         Check current state of menu item

         If current state of menu item <> preferred state of menu item then

              menu item.enabled = not menu item.enabled

         End If

    Next

    Thanks.


    John J. Bottiger (QA Engineer, Gladiator Innovations LLC)

    Tuesday, July 10, 2012 9:22 PM
  • I don't use TestPartner so it is a bit hard to for me to be sure of how to code what you want.

    I was hoping that someone else would reply but as that has not occurred I thought that I should make an attempt.

    However, the following code is tested in Excel. I used xl2002 rather than xl2007 or 2010 because I am assuming that TestPartner has the Toolbar type setup and not Ribbons as in xl2007/2010

    Don't know if it will help but I am hoping that it might put you on the right track. I don't know what output facilities you have in TestPartner so some or all of it might not be any good to you at all.

    Sub NamesOfCommandBars()
        'Return names of all of the CommandBars
        Dim i As Long
       
        'Column Headers
        Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = "CommandBar Names"
        Cells(Rows.Count, "A").End(xlUp).Font.Bold = True
       
        For i = 1 To Application.CommandBars.Count
            Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) _
                        = Application.CommandBars(i).Name
        Next i
    End Sub

    Sub CommandBarControls()
        'Return the Caption of a specific CommandBar and Enabled Status
        Dim ctrl As CommandBarControl
        Dim strComBarName As String
       
        strComBarName = "Worksheet Menu Bar"
       
        'Column Headers
        Cells(Rows.Count, "A").End(xlUp).Offset(2, 0) = strComBarName
        Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = "Enabled Status"
        Cells(Rows.Count, "A").End(xlUp).Font.Bold = True
        Cells(Rows.Count, "A").End(xlUp).Offset(0, 1).Font.Bold = True
       
        For Each ctrl In Application.CommandBars("Worksheet Menu Bar").Controls
            Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = ctrl.Caption
            Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = ctrl.Enabled
        Next ctrl
    End Sub

    Sub ToolBars()
        'Return all available toolbars
        Dim ctrl As CommandBarControl
        Dim strToolBar As String
       
        strToolBar = "Toolbar List"
       
        'Column Headers
        Cells(Rows.Count, "A").End(xlUp).Offset(2, 0) = "Toolbar Caption"
        Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = "Enabled Status"
        Cells(Rows.Count, "A").End(xlUp).Font.Bold = True
        Cells(Rows.Count, "A").End(xlUp).Offset(0, 1).Font.Bold = True

        For Each ctrl In Application.CommandBars(strToolBar).Controls
            Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = ctrl.Caption
            Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = ctrl.Enabled
        Next ctrl
    End Sub

    Sub ControlsInMenuItem()
        'Return items in Menu Item
        Dim ctrl As CommandBarControl
        Dim strCtrlCaption
       
        strCtrlCaption = "Edit"
       
        'Column Headers
        Cells(Rows.Count, "A").End(xlUp).Offset(2, 0) = "Edit Caption"
        Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = "Enabled Status"
        Cells(Rows.Count, "A").End(xlUp).Font.Bold = True
        Cells(Rows.Count, "A").End(xlUp).Offset(0, 1).Font.Bold = True
       
        For Each ctrl In Application.CommandBars(strCtrlCaption).Controls
            Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = ctrl.Caption
            Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = ctrl.Enabled
        Next ctrl

    End Sub

    Sub IndivCtrlInEditMenu()
        'Return and/or set Enabled status of an item
       
        Dim strCtrlCaption
       
        strCtrlCaption = "Edit"
       
        'Return Enabled stat of individual item in Edit Menu (Copy item)
        MsgBox Application.CommandBars(strCtrlCaption).Controls("&Copy").Enabled
       
        '*********************************************************************************
        'If run following then ensure to re-set to True
        If Application.CommandBars(strCtrlCaption).Controls("&Copy").Enabled = True Then
            Application.CommandBars(strCtrlCaption).Controls("&Copy").Enabled = False
        End If
       
        'No need to test for condition first. Just set to condition required
        'Will reset to True after previous code
        Application.CommandBars(strCtrlCaption).Controls("&Copy").Enabled = True

    End Sub


    Regards, OssieMac

    Wednesday, July 11, 2012 4:59 AM