• Toggle check box in customized menu (VBA Word 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Toggle check box in customized menu (VBA Word 2000)

    Author
    Topic
    #373367

    Not sure if this belongs in this forum or the Word forum, but here goes.

    I have a macro to toggle the display of a commandbar(“Menu Bar”). I’d like to assign it to another custom menu in the View menu in Word. No problem so far. However, I’d like to add a check mark to the left of the button when the toggle is true like is present for “Rulers” in Word’s “View” menu (I know, I just can’t leave well enough alone). Is that possible short of creating two menu options, one with a check mark as image and one without and toggling which one is visible? It does not appear that MS did that since the image for the “Rulers” menu item is actually a small envelope within a large envelope, which seems to have nothing to do with Rulers.

    Thanks for your help!!
    Troy

    Viewing 2 reply threads
    Author
    Replies
    • #599919

      You can use the State property for this. For toolbar buttons, this determines whether the button is “up” or “down”. For menu items, it determines whether the menu item is checked or not.

      Use code like the following:

      With CommandBars(“MyBar”).Controls(“MyMenu”).Controls(“MyItem”)
      If .State = msoButtonDown Then
      .State = msoButtonUp
      Else
      .State = msoButtonDown
      End If
      End With

    • #599920

      Hi Troy,

      I’ve just posted the below in the MS Word newsgroups today:

      If you want to change the state every time you press the button, then put the following code in the macro that is run:

      Dim myButton As CommandBarButton
      
      Set myButton = CommandBars.ActionControl
      If myButton.State = msoButtonDown Then
        myButton.State = msoButtonUp
        myButton.TooltipText = "Up!"
      Else
        myButton.State = msoButtonDown
        myButton.TooltipText = "Down!"
      End If
      
      ... or, in "shorthand":
      CommandBars.ActionControl.State = Not CommandBars.ActionControl.State

      cheersKlaus

    • #599940

      > I’d like to add a check mark to the left of the button when the toggle is true like is present for “Rulers”
      You need to set the FaceId = 1 and State = msoButtonUp when your commandbar is invisible and set the FaceId = 990 and State = msoButtonDown when the commandbar is visible. I used Tools | Customize to create a CommandBar called Customer and then added with a macro a menu item, Customer, just below the Ruler. Here are the macros that I used. HTH –Sam

      Option Explicit
      
      Sub AddCustomer()
      '   Add View | Customer menu item
      Dim c As CommandBarControl
          Set c = CommandBars("View").Controls.Add(Type:=msoControlButton, Before:=7)
          c.Caption = "Customer"
          c.DescriptionText = "Toggle Customer Toolbar"
          c.OnAction = "ToggleCustomer"
          c.Style = msoButtonIconAndCaption
          c.FaceId = 990
          c.State = msoButtonDown
          CommandBars("Customer").Visible = True
      End Sub
      
      Sub ToggleCustomer()
      '   Toggles Customer Toolbar
      Dim c As CommandBarControl
          Set c = CommandBars("View").Controls("Customer")
          With CommandBars("Customer")
              If .Visible Then
                  .Visible = False
                  c.FaceId = 1
                  c.State = msoButtonUp
              Else
                  .Visible = True
                  c.FaceId = 990
                  c.State = msoButtonDown
              End If
          End With
      End Sub
      
      Sub RemoveCustomer()
      '   Removes View | Customer menu item
      Dim c As CommandBarControl
          Set c = CommandBars("View").Controls("Customer")
          c.Delete
          CommandBars("Customer").Visible = False
      End Sub
      • #600085

        bow Sammy!! I already feared I hadn’t really understood the question.

        On a related note, MS seems to consider my code

        CommandBars.ActionControl.State = Not CommandBars.ActionControl.State

        sloppy programming, and I’d like to know why.
        They suugest WD2000: Sample Macros to Turn a True or False Property On or Off (Q198843)

           Sub ToggleTextBoundaries()
              n = ActiveWindow.View.ShowTextBoundaries
              ActiveWindow.View.ShowTextBoundaries = _
              Abs(ActiveWindow.View.ShowTextBoundaries = -n)
           End Sub 

        The values if you cast to numbers are True = -1, False = 0.
        I think I’ve read that MS planned to change the value of True to 1 in VB.Net, and the above code looks like a work-around.

        But “Not True” should still be “False”, shouldn’t it?

        I use “X = Not X” all the time for boolean variables, and if that’s bad style, I’d sure like to know (and why)!

        cheersKlaus

        • #600088

          Aristotle would have agreed with your viewpoint. I think it was he who coined “The law of the excluded middle”. Some modern systems of logic do not accept that Not-True = False and Not False = True.

          Maybe Microsoft were thinking of allowing True, False and Not Defined as valid values for a Boolean variable smile

          StuartR

          • #600096

            “Modern systems of logic”? Looking at that example code, I think you give MS much too much credit grin

            To me it looks more that they expect us to jump through hoops, so they can continue their sloppy programming aflame

            cheersKlaus

            • #600108

              The introduction to the sample macro makes a big fuss about the importance of Abs:
              [indent]


              This method uses the Abs function, which specifies the absolute value of a number, where True equals -1 and False equals 0 (zero)


              [/indent]
              Yet, Abs seems totally redundant to me, and the macro works fine without it.

              And the (undeclared) variable n also seems wholly redundant:

              ActiveWindow.View.ShowTextBoundaries = _
                    (ActiveWindow.View.ShowTextBoundaries = False)

              works quite as well.

              confusedConfused?
              You bet!

              brainwashKlaus

        • #600120

          > MS seems to consider my code sloppy programming
          It seems to me that MS always tries to make boolean variables more difficult. The State variable actually has three possible values, but if your code works, then I would use it. It’s clear that your code toggles the state. If MS cannot document msoButtonMixed, then why should we bother with msoButtonUp and msoButtonDown. Oh well, now I’ve lost that job opportunity at Redmond. crybaby –Sam

          • #600126

            Thank you for your support. Reading the MS sample code, I had the feeling that I was slowly going bonkers.

            Read it once, read it twice, read it three times and couldn’t make any heads or tails from it, as if I had suddenly unlearned everything about VBA. Now I’m ok again…
            newbrainKlaus

            • #601681

              Returning this thread to its original context… pirate

              I have a number of toolbar buttons that toggle different things. It is easy to set the state of the button in the Macro that fires when the user clicks the button, but I need to keep the button updated as the user does other things in Word.

              Some of the buttons are document specific, so I can update them from a DocumentChange event in a class module. One button has information about the current paragraph, does anyone have any ideas on what event(s) I can use to ensure I keep this censored button in synch with the current paragraph format?

              StuartR

            • #601698

              Replying to my own post…

              I have something working, using Application.Ontime to check for changes every second.

              I wrote the code to exit very rapidly if there hasn’t been a change of state and I checked in Task Manager to see how much CPU word was using, it doesn’t seem too bad. I can’t decide if the responsiveness of a once a second check is good enough, but it’s probably acceptable.

              StuartR

    Viewing 2 reply threads
    Reply To: Toggle check box in customized menu (VBA Word 2000)

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

    Your information: