Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Microsoft Word 2007中通过VBA宏显示右键单击上下文菜单,并将焦点放在中_Vba_Ms Word_Contextmenu_Office 2007 - Fatal编程技术网

在Microsoft Word 2007中通过VBA宏显示右键单击上下文菜单,并将焦点放在中

在Microsoft Word 2007中通过VBA宏显示右键单击上下文菜单,并将焦点放在中,vba,ms-word,contextmenu,office-2007,Vba,Ms Word,Contextmenu,Office 2007,我希望通过Word2007中的VBA宏以编程方式显示右键单击上下文菜单,并将焦点放在其中 这将允许我将宏映射到热键,并在不离开键盘的情况下显示具有焦点的菜单。我假设这将通过应用程序对象的命令栏集合完成,通过以下方式访问: Application.commandbar.“在此处访问适当的方法或成员” 但我没有看到任何方法或成员会显示上下文菜单。是否可以通过VBA宏实现这一点 编辑: 按照建议,我循环遍历了每个CommandBar并获得了名称和索引,以尝试找出要使用的CommandBar索引: Su

我希望通过Word2007中的VBA宏以编程方式显示右键单击上下文菜单,并将焦点放在其中

这将允许我将宏映射到热键,并在不离开键盘的情况下显示具有焦点的菜单。我假设这将通过
应用程序
对象的
命令栏
集合完成,通过以下方式访问:

Application.commandbar.“在此处访问适当的方法或成员”

但我没有看到任何方法或成员会显示上下文菜单。是否可以通过VBA宏实现这一点

编辑:

按照建议,我循环遍历了每个CommandBar并获得了名称和索引,以尝试找出要使用的CommandBar索引:

Sub C_RightClick()
'Activates right-click context menu
'

Dim cbar As Office.CommandBar
Dim cbarIndex As Integer
Dim testString As String
Dim cBarsArray(0 To 500)
Dim arrayCounter As Integer

testString = ""
arrayCounter = 1

For Each cbar In CommandBars
    'TRUE if right-click
    'If LCase(cbar.Name) = 'right-click' Then
    '    cbarIndex = cbar.Index
    'End If

    testString = testString + CStr(cbar.Index) + " " + cbar.Name + " " + CStr(cbar.Type = msoBarTypePopup) + vbCrLf
    Debug.Print cbar.Name; " "; cbar.Type = msoBarTypePopup

    'Add name to array and increment counter
    cBarsArray(arrayCounter) = cbar.Name
    arrayCounter = arrayCounter + 1

Next cbar

MsgBox testString

'Application.CommandBars(cbarIndex).ShowPopup


End Sub
但是,我没有看到任何标题为“右键单击”的内容。我认为它可能是“标准”,其索引为1,但在尝试访问它时收到了一个错误

如果任何人知道当选择
Home
选项卡时Word 2007中出现的默认右键单击上下文菜单的正确名称,将不胜感激。否则,我会把这个问题交给超级用户,自己研究。谢谢您的帮助。

尝试以下方法:

Application.CommandBars(100).ShowPopup
Application.CommandBars(100).Controls("Paste").Execute
参数可以是命令栏索引或标题

要在命令栏上执行特定命令,请尝试以下操作:

Application.CommandBars(100).ShowPopup
Application.CommandBars(100).Controls("Paste").Execute
要将所有命令栏的列表打印到即时窗口,请执行以下操作:

Sub test()
Dim cbar As Office.CommandBar
For Each cbar In CommandBars
    'TRUE if right-click
    Debug.Print cbar.Name; " "; cbar.Type = msoBarTypePopup
Next cbar
End Sub
编辑: 在回答您关于右键单击菜单的问题时,我认为它是一种与CommandBar不同的控件

为了更好地了解右键单击菜单的名称和索引,我对上面的代码做了一些修改。现在尝试向每个右键单击菜单添加控件。添加的控件的标题是菜单的名称和索引。这些控件是临时的-下次打开Word时它们将消失

Sub test()
Dim cbar As Office.CommandBar
Dim ctl As Office.CommandBarControl
For Each cbar In Application.CommandBars
    With cbar
        On Error Resume Next
        'this will delete any customizations
        .Reset
        Set ctl = .Controls.Add(Type:=msoControlButton, Temporary:=True)
        ctl.Caption = .Index & " - " & cbar.Name
        Debug.Print "Name: "; cbar.Name; " Right-click: "; cbar.Type = msoBarTypePopup; " Error descr: "; Err.Description
        On Error GoTo 0
    End With
Next cbar
End Sub
它还将错误消息(如果有)打印到即时窗口

我认为“主页”上下文菜单不会给您带来好运的原因是它没有添加任何控件。下面是添加控件的菜单的图片:


有关主页选项卡菜单上的想法,请参见我的编辑。