我创建了一个自定义 Word 功能区选项卡,具有三个项目符号切换按钮。
虽然这些按钮在样式方面起作用,但当我位于不同的行时,它们看起来被按下,这与“主页”选项卡中的项目符号按钮的行为不同。
如何为我的自定义选项卡实现相同的行为?
我点击按钮来取消按下它,即使我在没有子弹的线上。
请注意,每个项目符号样式都链接到 Word 样式。
我为每种项目符号样式都有一个子项目,总共三个脚本。
以下是样式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
以下是切换操作的回调,您可以在其中找到所有按钮及其对应的项目符号样式。
'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
这是切换按钮
getpressed
的回调,需要改进以解决切换按钮所表现出的行为。
'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 中的文本和光标位置变化页面很有帮助。
在实现切换按钮功能时,尽管添加了事件处理程序并使用