【发布时间】:2019-11-24 18:50:57
【问题描述】:
这是我有史以来第一个关于 SO 的问题,即使我经常来这里(直到今天我一直都能找到答案)。我知道我已经发布了这个问题,但由于某种原因我不适合我。
我正在尝试获得一个右键单击子菜单,其中包含我的 word 文档中每个编号的项目的列表。它的目的是在我的文档中插入我的编号项目的编号和内容文本。
问题是我不知道如何影响每个 .OnAction(在我的文档中插入编号的项目)和每个 .Caption(在我的菜单中显示我的编号项目的编号和内容文本)具有不同的变量(每个编号的项目一个)。我的报价可能有问题,但我看不到任何其他解决方案。
我的代码如下:
Option Explicit
Sub ControlButtonNumberedItems()
'Parameters for NumberedItems
Dim i As Integer
i = 1
Dim NumberedItems As Integer
NumberedItems = ActiveDocument.CountNumberedItems
'Parameters for CommanBar
Dim MenuButton As CommandBar
Set MenuButton = Application.CommandBars("Text")
Dim SubMenuButton As CommandBarControl
Set SubMenuButton = MenuButton.Controls.Add(Type:=msoControlPopup, Before:=1)
With SubMenuButton
.Caption = "NumberedItems"
.Tag = "My_Tag"
While i <= NumberedItems
With .Controls.Add(Type:=msoControlButton)
.OnAction = "'InsertNumberedItem""i""'"
.FaceId = 38
.Caption = "MyCaption"
End With
i = i + 1
Wend
End With
End Sub
Sub InsertEvidence(i As Integer)
'Insert NumberRelativeContext
Selection.InsertCrossReference ReferenceType:=wdRefTypeNumberedItem, _
ReferenceKind:=wdNumberRelativeContext, _
ReferenceItem:=i, _
InsertAsHyperlink:=True, _
SeparatorString:=" "
Selection.TypeText Text:=" "
'Insert ContentText
Selection.InsertCrossReference ReferenceType:=wdRefTypeNumberedItem, _
ReferenceKind:=wdContentText, _
ReferenceItem:=i, _
InsertAsHyperlink:=True, _
SeparatorString:=" "
'Text form
Selection.Expand Unit:=wdLine
Selection.Font.Bold = wdToggle
Selection.Font.Italic = wdToggle
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.ParagraphFormat.SpaceBefore = 6
End Sub
Sub ResetRightClick()
Application.CommandBars("Text").Reset
End Sub
提前感谢您的帮助。如果您需要任何其他信息,请告诉我。
【问题讨论】:
-
stackoverflow.com/questions/13079727/…
.OnAction = "'InsertNumberedItem " & i & "'" -
谢谢蒂姆!但是,如果这是正确的语法,我的宏中似乎还有另一个问题,因为我收到此错误消息:“La macro est introuvable ou a été désactivée en raison de vos paramètres de sécurité des macros”(“The macro由于您的宏安全设置,未找到或已被禁用”英文)。奇怪的是,如果我不在 .OnAction 中插入任何参数,我的宏就可以完美运行。知道它来自哪里吗?
-
您确定要在宏名称后和参数前留一个空格吗?
-
不幸的是问题仍然存在......我仍然一遍又一遍地收到相同的错误消息。这个宏对你有用吗?我的 Word 版本有问题吗(我有 Word office 365Proplus)?谢谢你的帮助。 PS:“.onaction”(InsertNumberedItem)中指示的Sub的名称与我的代码(InsertEvidence)中的名称不匹配,我已经修复了这个错误。
-
我这几天无法测试 - 无法访问 PC
标签: vba ms-word cross-reference