VB.Net 相当于 VBA 中的 (CustomizationContext)

VB.Net equivalent for (CustomizationContext) in VBA

我正忙于一些文字自动化,运行 遇到一个问题,即文档中的上下文菜单中包含我希望删除的项目。

文档打开后,通过 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" 的上下文菜单子项都将被删除。

当我尝试将此代码移动到我的 VB.NET 解决方案时(我无法选择语言,所以 VB 它是),我在 CustomizationContext = ActiveDocument 行上遇到错误(这必须有行才能影响当前文档)。

我得到的错误是CustomizationContext' is not a by reference property.

有谁知道如何得到 vb.net 的那一行?

提前致谢。

编辑:如果您需要查看 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

Jules 用他的示例代码为我指明了正确的方向。 经过大量尝试后,我注意到在解决方案的某处,WordApplication 的 [TYPE] 被更改为动态类型,因此,它无法使用 CustomizationContext.

我的解决方案是这样的:

我更改了这一行;

WordApplication.CustomizationContext = WordApplication.ActiveDocument

为此:

CType(WordApplication, Microsoft.Office.Interop.Word.Application).CustomizationContext = WordApplication.ActiveDocument

强制类型正确。

简单的解决方案,但需要一些时间。

感谢 Jules 为我指明了正确的方向。 (积分应该给你)。