Toggle button on toolbars

H

Harry-Wishes

Hello

I have programmatically created a toggle feature that displays and hides a
custom toolbar in Word. I have successfully attached the macro to a button on
a second toolbar such that, when pressed, it cause the other toolbar to
alternate its display(visible/hidden). See code below. Works great.

Sub ToggleToolBar()

If Application.CommandBars("GRAS_Template").Visible = False Then
Application.CommandBars("GRAS_Template").Visible = True
Else
Application.CommandBars("GRAS_Template").Visible = False
End If

End Sub

The step I am currently researching is the possibility of changing the
display name of the button to reflect the visibility status of the toolbar.
Is there a programmatic way of doing this? Example: Display name toggles
from "ON" to "OFF" and visa versa.

Thanks

Harry-Wishes
 
C

Cindy M.

Hi Harry,
The step I am currently researching is the possibility of changing the
display name of the button to reflect the visibility status of the toolbar.
Is there a programmatic way of doing this? Example: Display name toggles
from "ON" to "OFF" and visa versa.
Try something like the following. Note that I've used the Tag property to
identify the toggle button control, rather than the caption (which changes)

Sub ToggleToolBar()
Dim ctlToggle As Office.CommandBarButton

Set ctlToggle = CommandBars("Test-Test").FindControl(Tag:="ToggleTest")
If Application.CommandBars("Test").Visible = False Then
Application.CommandBars("Test").Visible = True
ctlToggle.Caption = "On"
Else
Application.CommandBars("Test").Visible = False
ctlToggle.Caption = "Off"
End If

End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
H

Harry-Wishes

Thanks Cindy

I did try using the set command using the actual names of my existing
toolbars. Unfortunately, the macro has trouble when assigning the caption
name to the button, hanging up on ctlToggle.Caption property.

I recieve the following message:

Run-time error '91' Object variable or With block not set




Dim ctlToggle As Office.CommandBarButton
Set ctlToggle = CommandBars("Test").FindControl(Tag:="ToggleTest")


If Application.CommandBars("GRAS_Template").Visible = False Then
Application.CommandBars("GRAS_Template").Visible = True
ctlToggle.Caption = "On" ''''RUN-TIME ERROR 91
Else
ctlToggle.Caption = "Off" ''''RUN-TIME ERROR 91
Application.CommandBars("GRAS_Template").Visible = False
End If
End Sub
 
H

Harry-Wishes

Hi Cindy

After some brainstorming, I found the problem. In the 'Set' command, you
had the wrong method when accessing the control button of the CommandBar
object. Instead of using the FindControl method in your original code, I
found that the Controls method actually locates the button. Since I have
only one button on the toolbar, I know the index number has to be 1.

Set ctlToggle = CommandBars("Test-Test").Controls(1)

This was good. Something new to learn each today.
Thanks for your help.
 
C

Cindy M.

Hi Harry,
After some brainstorming, I found the problem. In the 'Set' command, you
had the wrong method when accessing the control button of the CommandBar
object.
Well, no, it did work for me here. I did test it a few times (Word 2007).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top