Results 1 to 12 of 12
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Toggle check box in customized menu (VBA Word 2000)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    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

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    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:
    <pre>
    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</pre>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Toggle check box in customized menu (VBA Word 2000)

    > 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
    <pre>Option Explicit

    Sub AddCustomer()
    ' Add View | Customer menu item
    Dim c As CommandBarControl
    Set c = CommandBars("View").Controls.Add(Type:=msoControlB utton, 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</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    <img src=/S/bow.gif border=0 alt=bow width=15 height=15> Sammy!! I already feared I hadn't really understood the question.

    On a related note, MS seems to consider my code
    <pre>CommandBars.ActionControl.State = Not CommandBars.ActionControl.State</pre>

    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)

    <pre> Sub ToggleTextBoundaries()
    n = ActiveWindow.View.ShowTextBoundaries
    ActiveWindow.View.ShowTextBoundaries = _
    Abs(ActiveWindow.View.ShowTextBoundaries = -n)
    End Sub </pre>


    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)!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    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 <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    StuartR

  7. #7
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    "Modern systems of logic"? Looking at that example code, I think you give MS much too much credit <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    To me it looks more that they expect us to jump through hoops, so they can continue their sloppy programming <img src=/S/aflame.gif border=0 alt=aflame width=15 height=15>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    The introduction to the sample macro makes a big fuss about the importance of Abs:
    <hr>This method uses the Abs function, which specifies the absolute value of a number, where True equals -1 and False equals 0 (zero)<hr>
    Yet, Abs seems totally redundant to me, and the macro works fine without it.

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

    <pre>ActiveWindow.View.ShowTextBoundaries = _
    (ActiveWindow.View.ShowTextBoundaries = False)</pre>

    works quite as well.

    <img src=/S/confused.gif border=0 alt=confused width=15 height=20>Confused?
    You bet!

    <img src=/S/brainwash.gif border=0 alt=brainwash width=15 height=15>Klaus

  9. #9
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Toggle check box in customized menu (VBA Word 2000)

    > 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. <img src=/S/crybaby.gif border=0 alt=crybaby width=15 height=15> --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  10. #10
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    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...
    <img src=/S/newbrain.gif border=0 alt=newbrain width=21 height=22>Klaus

  11. #11
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    Returning this thread to its original context... <img src=/S/pirate.gif border=0 alt=pirate width=22 height=18>

    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 <img src=/w3timages/censored.gif alt=censored border=0> button in synch with the current paragraph format?

    StuartR

  12. #12
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Toggle check box in customized menu (VBA Word 2000)

    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

Posting Permissions

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