Hi Greg,
Apologies - the cc is an overflow of a comment on the previous line which I
should have deleted - it's the remnants of earlier code and not relevant at
all. mySetting is also an overflow due to the line breaks added by the
system. I have reformatted in the code below to stop that happening (I
hope).
I just assumed you created your toolbar with VBA but if you've done it via
the UI then you can't make a control hidden. I guess I'd do it with a
one-off statement in the immediate window - whatever, it needs VBA.
The new code adds an optional override to the toggle setting so, for
example, you could do ..
ToggleState CommandBars("Bookmarker").Controls("Hide"), "Visible", False
to force the setting of visible to false (i.e. hide the control) instead of
toggling what was there before. myOverride might have been a better choice
of name than mySetting.
The extra parameter is optional so that the original still worked, in other
words ..
ToggleState CommandBars("Bookmarker").Controls("Hide"), "Visible"
still toggled the setting.
To use it (somewhat artificially) in your example you could be explicit in
what you set rather than toggling. I think you've got the check wrong so
I've changed it in this ..
Sub ToggleShowHide()
ToggleState CommandBars("MyToggle").Controls("Bold On"), "Visible", False
If CommandBars("MyToggle").Controls("Bold Off").Visible = False Then
ActiveDocument.Range.Bold = True
Else
ActiveDocument.Range.Bold = False
End If
ToggleState CommandBars("MyToggle").Controls("Bold Off"), "Visible", True
End Sub
Sub ToggleState(myControl As CommandBarControl, _
myState As String, _
Optional mySetting)
Dim SavedState As Boolean
Dim OldState As Boolean
Dim ControlTemplate As Template
Set ControlTemplate = Templates(myControl.Parent.Context)
SavedState = ControlTemplate.Saved
OldState = CallByName(myControl, myState, VbGet)
If Not IsMissing(mySetting) Then
If TypeName(mySetting) = "Boolean" Then OldState = Not mySetting
End If
CallByName myControl, myState, VbLet, Not OldState
ControlTemplate.Saved = SavedState
End Sub
It's your code, but I would actually do it more simply ..
Sub ToggleShowHide()
With CommandBars("MyToggle")
ActiveDocument.Range.Bold = .Controls("Bold On").Visible
ToggleState .Controls("Bold On"), "Visible"
ToggleState .Controls("Bold Off"), "Visible"
End With
End Sub
The possible permutations are endless - you could, perhaps, make the
ToggleState procedure automatically toggle two buttons at a time (as that's
what you're always doing - and probably always will) - or you could give it
two buttons and tell it to ensure one is true, the other false, etc., etc. -
the world, as they say, is your lobster (or something of the sort). As
already agreed, if it works don't fix it.