I created a custom Word ribbon tab, featuring three bullet toggle buttons.
While these buttons function in terms of styles, they appear pressed when I'm on a different line, unlike the behavior of the bullets button in the Home tab.
How do I achieve the same behavior for my custom tab?
I click on the button to unpress it, even when I'm on a line without bullets.
Note that each bullet style is linked to a Word style.
I have a Sub for each bullet style, three scripts in total.
The following is for style2 (bullet "-")
Sub BulletListStyle2()
'This macro applies the Style 'bullet stlyle 2' to text
Set Doc = ThisDocument
With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = ChrW(8208) ' hyphen "-"
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = CentimetersToPoints(1)
.Alignment = wdListLevelAlignLeft
.TextPosition = CentimetersToPoints(1.5)
.TabPosition = CentimetersToPoints(0.5)
.ResetOnHigher = 0
.StartAt = 0
With .Font
.Name = "Calibri"
End With
.LinkedStyle = "Bullet style 2"
End With
ListGalleries(wdBulletGallery).ListTemplates(1).Name = "Bullet style 2"
Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior
End Sub
The following is the callback for the toggle-on action, where you can find all the buttons and their corresponding bullet styles.
'Callback for Toogle onAction
Sub ToggleonAction(control As IRibbonControl, pressed As Boolean)
Dim ListTemplatesCount As Integer
On Error Resume Next ' Enable error handling
Dim Doc As Document
Set Doc = ActiveDocument
If Not Doc Is Nothing Then
Select Case control.ID
Case Is = "TB11" 'button for 'bullets style 2'
If pressed Then
Call BulletListStyle1
Else
' Perform actions when button is not pressed
Selection.Range.ListFormat.RemoveNumbers
End If
Case Is = "TB12". 'button for 'bullets style 3'
If pressed Then
Call BulletListStyle2
Else
' Perform actions when button is not pressed
Selection.Range.ListFormat.RemoveNumbers
End If
Case Is = "TB4". 'button for 'bullets style'
If pressed Then
' Perform actions when button is pressed
Call BulletListStyle0
Else
' Perform actions when button is not pressed
Selection.Range.ListFormat.RemoveNumbers
End If
End Select
'Force the ribbon to redefine the control with correct image and label
myRibbon.InvalidateControl control.ID
Else
MsgBox "Active document Not found.", vbExclamation
End If
On Error GoTo 0 ' Disable error handling
End Sub
This is the callback for the toggle button getpressed
, which requires improvement to address the behaviour exhibited by the toggle button.
'Callback for togglebutton getPressed
Sub buttonPressed(control As IRibbonControl, ByRef toggleState)
'toggleState (i.e., true or false) determines how the 'toggle appears _
on the ribbon (i.e., flusn or sunken).
Dim ListTemplatesCount As Integer
ListTemplatesCount = ListGalleries(wdBulletGallery).ListTemplates.count
Select Case control.ID
Case Is = "TB11". 'Toggle button for style
toggleState = Selection.Range.ListFormat.ListType = wdListBullet
Case Is = "TB12"
toggleState = Selection.Range.ListFormat.ListType = wdListBullet
Case Is = "TB4"
' Determine the initial toggle state based on the current selection
toggleState = Selection.Range.ListFormat.ListType = wdListBullet
End Select
End Sub
While implementing the toggle button functionality, I encountered unexpected behavior despite adding the event handler and using <myRibbon.invalidate>. The resulting button acted like a regular button, not a toggle. To address this, I simplified the approach by changing the XML button type to a standard button and streamlining the VBA code. Unfortunately, this didn't lead to the desired toggle functionality.