locked
How to Add menu item to Office Context menu? RRS feed

  • Question

  •  

    Hi

    I am using VSTO for Word 2003 Add-In.

    I want to add menu item to the existing context menu of Office.

    Is this possible?

    Currently I am adding menu item through the WindowsBeforeRightClick event. But this either shows the Office menu after click of my context menu or if I set Cancel to true it does not appear.

    But I wanted to add My Menu Item to existing Context menu I don't want to disable Original context menu.

     

    Please suggest me solution.

     

    Thanks,

    Priyanka.

    Tuesday, May 13, 2008 11:27 AM

Answers

  •  Priyanka_choudhari wrote:

    I had added this code to my add-in.

    Though it executed silently I could not able to see my menu item When I RightClick.

    Dim _ContextMenu As Office.CommandBar = Application.CommandBars("ActiveX Control")

    If Not (_ContextMenu Is Nothing) Then

    cmdZafeContextMenu = _ContextMenu.Controls.Add(Office.MsoControlType.msoControlButton, Missing, Missing, Missing, True)

    If Not (cmdZafeContextMenu Is Nothing) Then

    With cmdZafeContextMenu

    .Caption = "Quick Zafe"

    .DescriptionText = "Quick Zafe the File"

    .TooltipText = "Quick Zafe this File"

    .Style = MsoButtonStyle.msoButtonCaption

    .Visible = True

    .Enabled = True

    End With

    End If

    End If

     

    Could you tell me where did I went wrong?

    Is your code in a Try...Catch block? If it is not, you need to put it in one. That's the only way you can be certain you're seeing any error messages the Office application may be generating.

     

    Have you tried running this code in Debug mode, with a break point? That's the only way you can tell whether your object variables (_ContextMenu and cmdZafeContextMenu) are "null" or not.

     

    Finally, what are you right-clicking on in the Word document? I have trouble imagining why the user would expect to save the file when you right-click an Office ActiveX control... Are you certain this is the context menu that should display when you right-click whatever it is in the document?

     

    Without this debugging information there's not a lot we can even guess at. Oh, and make sure you're explicitly setting the Application.CustomizationContext property to whichever file container in Word should be holding these CommandBar customizations (probably you want NormalTemplate).

    Thursday, May 15, 2008 3:22 PM

All replies

  • The first thing you have to determine is the CommandBar NAME property of the context menu you want to alter. Keep in mind that Word has many, many, many context menus and that you may need to add your command to more than one in order to ensure it will appear under all circumstances.

     

    In order to see the built-in Context menus, go to Tools/Customize/Toolbars. You should find an entry in that list for "Shortcut menus" (or something like that - I don't have Word 2003 or earlier installed on this machine, so I'm writing from memory). Activate the checkbox for that entry and a new toolbar should appear over the document with three buttons (dropdown menus). These are the main context menu categories (Text, Tables and Pictures, I think). In these lists you'll find all the context menus - the labels you see are also the Name property for the CommandBar to which the context menu belongs.

     

    Once you have this information, you instantiate a CommandBar object (  Dim cb as Office.CommandBar = wdApp.CommandBars("NameOfContextMenu")  ). Now you can add your control to this CommandBar (and don't forget to remove it when your Add-in unloads!).

     

    Tuesday, May 13, 2008 4:18 PM
  • Hi,

    I had added this code to my add-in.

    Though it executed silently I could not able to see my menu item When I RightClick.

    Dim _ContextMenu As Office.CommandBar = Application.CommandBars("ActiveX Control")

    If Not (_ContextMenu Is Nothing) Then

    cmdZafeContextMenu = _ContextMenu.Controls.Add(Office.MsoControlType.msoControlButton, Missing, Missing, Missing, True)

    If Not (cmdZafeContextMenu Is Nothing) Then

    With cmdZafeContextMenu

    .Caption = "Quick Zafe"

    .DescriptionText = "Quick Zafe the File"

    .TooltipText = "Quick Zafe this File"

    .Style = MsoButtonStyle.msoButtonCaption

    .Visible = True

    .Enabled = True

    End With

    End If

    End If

     

    Could you tell me where did I went wrong?

     

    Thanks.

    Priyanka.

    Thursday, May 15, 2008 8:42 AM
  •  Priyanka_choudhari wrote:

    I had added this code to my add-in.

    Though it executed silently I could not able to see my menu item When I RightClick.

    Dim _ContextMenu As Office.CommandBar = Application.CommandBars("ActiveX Control")

    If Not (_ContextMenu Is Nothing) Then

    cmdZafeContextMenu = _ContextMenu.Controls.Add(Office.MsoControlType.msoControlButton, Missing, Missing, Missing, True)

    If Not (cmdZafeContextMenu Is Nothing) Then

    With cmdZafeContextMenu

    .Caption = "Quick Zafe"

    .DescriptionText = "Quick Zafe the File"

    .TooltipText = "Quick Zafe this File"

    .Style = MsoButtonStyle.msoButtonCaption

    .Visible = True

    .Enabled = True

    End With

    End If

    End If

     

    Could you tell me where did I went wrong?

    Is your code in a Try...Catch block? If it is not, you need to put it in one. That's the only way you can be certain you're seeing any error messages the Office application may be generating.

     

    Have you tried running this code in Debug mode, with a break point? That's the only way you can tell whether your object variables (_ContextMenu and cmdZafeContextMenu) are "null" or not.

     

    Finally, what are you right-clicking on in the Word document? I have trouble imagining why the user would expect to save the file when you right-click an Office ActiveX control... Are you certain this is the context menu that should display when you right-click whatever it is in the document?

     

    Without this debugging information there's not a lot we can even guess at. Oh, and make sure you're explicitly setting the Application.CustomizationContext property to whichever file container in Word should be holding these CommandBar customizations (probably you want NormalTemplate).

    Thursday, May 15, 2008 3:22 PM