我为客户创建了一个自定义 Word 功能区选项卡,其中包含三个不同的项目符号切换按钮。虽然这些按钮在样式方面功能正常,但即使我位于不同的行,它们也会显示为按下状态,这与“主页”选项卡中的项目符号按钮的行为不同。我正在寻求有关为我的自定义选项卡实现相同行为的指导。
目前,即使我在没有子弹的线上,我也必须单击按钮才能取消按下它。有人可以帮助我正确编码 VBA 脚本以在按下时设置切换按钮吗?我正在努力解决这个问题,非常感谢专家的帮助。
参见显示切换按钮行为的图像:
我已经尝试了附加的代码,它在功能方面完成了工作,但在设计/与 VBA 代码交互方面需要改进
VBA脚本:请注意,每个项目符号样式都链接到一个单词样式我每个项目符号样式都有一个子,总共3个脚本,以下是样式2(项目符号“-”)
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
以下是切换操作的回调,您可以在其中找到所有按钮及其相应的项目符号样式。随后,有切换按钮
getpressed
的回调,需要改进以防止切换按钮表现出奇怪的行为。
'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
'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
要刷新功能区 UI 状态,您需要使用 IRibbonUI 界面的 Invalidate 或 InvalidateControl 方法。这些方法使功能区用户界面的所有控件(或特定控件)的缓存值无效。
例如,如果外接程序编写器为按钮实现
getPressed
回调过程,则调用该函数一次,加载状态,然后如果需要更新状态,则使用缓存的状态而不是调用程序。此过程将保持不变,直到加载项使用 Invalidate
方法发出信号表明缓存的值无效,此时,再次调用回调过程并缓存返回响应。然后,加载项可以通过调用 Refresh
方法强制立即更新 UI。
在功能区 XML 中,您可以添加以下回调来获取
IRibbonUI
接口的实例:
<customUI … OnLoad="MyAddinInitialize" …>
然后在代码中您可以定义以下回调:
Dim MyRibbon As IRibbonUI
Sub MyAddInInitialize(Ribbon As IRibbonUI)
Set MyRibbon = Ribbon
End Sub
Sub myFunction()
MyRibbon.Invalidate() ' Invalidates the caches of all of this add-in's controls
End Sub
在您的情况下,您可以使用 Document.SelectionChange 事件,该事件在文档窗口中的选择更改时触发,以调用
Invalidate
方法。
最后,您可能会发现如何使用 VSTO 检测 Word 中的文本和光标位置变化页面很有帮助。