Search code examples
vbams-wordcontextmenuoffice-2007

Display right click context menu with focus in through VBA macro in Microsoft Word 2007


I would like to programmatically display the right click context menu with focus through a VBA macro in Word 2007.

This would allow me to map the macro to a hotkey and expose the menu with focus without leaving the keyboard. I assumed this would be done through the Application object's CommandBars collection, accessed along the lines of :

Application.CommandBars.'access appropriate mehod or member here'

But I do not see any method or member that seems like it would show the context menu. Is it possible to achieve this through a VBA macro?

EDIT:

As suggested I looped through each CommandBar and obtained the name and index to try and find out which CommandBar index to use:

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

However, I do not see any titled 'Right-Click'. I thought it may be 'Standard' , whose index is 1, but received an error when I attempted to access it.

If anyone knows the correct name for the default right-click context menu that appears in Word 2007 when the Home tab is selected, it would be appreciated. Otherwise, I will take that question to SuperUser and research on my own. Thank you for the help.


Solution

  • Try something like:

    Application.CommandBars(100).ShowPopup
    

    The argument can be the Commandbar index or caption.

    To execute a particular command on a commandbar, try something like:

    Application.CommandBars(100).Controls("Paste").Execute
    

    To print a list of all commandbars to the Immediate Window:

    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
    

    EDIT: In answer to your question about the right-click menu that you get over the HOME tab, I think it's a different kind of control from CommandBar.

    To get a better idea of the right-click menu names and indexes, I've modified the code above slightly. This now tries to add a control to each right-click menu. The added control's caption is the menu's name and Index. The controls are temporary - they'll be gone the next time you open 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
    

    It also prints out the error message, if there was one, to the immediate window.

    The reason I don't think you'll have luck with the "Home" context menu is that no control is added to it. Here's a pic of a menu with the control added:

    enter image description here