I had occasion to toggle buttons as specific tasks were performed. In this case I am harvesting contact-management text from a document.
Once the user has used the macro "GetSurn" to harvest the Surnname, I want to toggle the macro button OFF.
When a fresh record is started, I want to reset all the buttons to ON.
Code: Select all
Function HighlightButton(strToolbar As String, Optional strCaption, Optional blnSet)
'''
Application.CustomizationContext = ThisDocument
Dim lngCB As Long
For lngCB = 1 To Application.CommandBars.Count
Dim cb As CommandBar
Set cb = Application.CommandBars(lngCB)
If cb.Name = strToolbar Then
Dim lngCtl As Long
For lngCtl = 1 To cb.Controls.Count
Dim ctl As CommandBarControl
Set ctl = cb.Controls(lngCtl)
If IsMissing(strCaption) Then
If IsMissing(blnSet) Then
ctl.Enabled = Not ctl.Enabled
Else
ctl.Enabled = blnSet
End If
Else
If Right(ctl.Caption, Len(strCaption)) = strCaption Then
If IsMissing(blnSet) Then
ctl.Enabled = Not ctl.Enabled
Else
ctl.Enabled = blnSet
End If
Else
End If
End If
Next lngCtl
Else
End If
Next lngCB
'Sub TESTHighlightButton()
' Call HighlightButton("Snare", "GetSurn", True)
' Call HighlightButton("Snare", "GetSurn")
' Call HighlightButton("Snare", "GetSurn", True)
' Call HighlightButton("Snare", "GetSurn", False)
' Call HighlightButton("Snare", "GetSurn")
' Call HighlightButton("Snare", , True)
' Call HighlightButton("Snare", , False)
' Call HighlightButton("Snare", "GetSurn", True)
' Call HighlightButton("Snare")
'End Sub
End Function