【发布时间】:2016-04-25 12:37:02
【问题描述】:
我正忙于一些单词自动化,并且遇到了一个问题,即文档中的上下文菜单包含我希望删除的项目。
打开文档后,通过 vba,我可以通过运行以下代码来删除这些项目;
[VBA]
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
CustomizationContext = ActiveDocument
For Each oContextMenu In ActiveDocument.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete
End If
Next
End If
Next
如果我执行此代码并检查文档,则会删除所有包含文本“smokeball”的 contextMenu 子项。
当我尝试将此代码移至我的 VB.NET 解决方案时(我没有选择语言,所以是 VB),我在 CustomizationContext = ActiveDocument 行上收到错误(此行必须存在才能影响当前文档)。
我得到的错误是CustomizationContext' is not a by reference property.
有谁知道如何获得与 vb.net 等效的 ONE 行?
提前致谢。
编辑:如果您需要查看 vb.net 子:
Private Sub RemoveUnwantedContextMenuItems()
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
WordApplication.CustomizationContext = WordApplication.ActiveDocument 'This is the error.
For Each oContextMenu In WordApplication.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete()
End If
Next
End If
Next
End Sub
PS - 我也已经尝试过使用.AttachedTemplate 和.Normal / .NormalTemplate
【问题讨论】:
-
我希望谁曾给这个问题打过 -1 分,请给出一些解释...已经花了将近一天半的时间来研究这个主题。
-
WordApplication 是否已初始化并打开?
-
@Jules 是的,现阶段我已经在其他领域使用它了。
-
ActiveDocument 在这个阶段是否仍然打开?在问题行之前尝试 Console.WriteLine(WordApplication.Documents.Count)。
-
奇怪,我无法复制这里的问题是我使用的代码codeshare.io/Y4g31
标签: vb.net vba ms-word automation interop