none
How do I subscribe an event handler to the ContextMenuOpening event in MS Word? RRS feed

  • Question

  • I am trying to call a method any time a context menu is opened in Word.  I understand that the method has to have the same signature as the event's delegate.  I was hoping it was as simple as this:

    FrameworkElement.ContextMenuOpeningEvent += MethodName;

    But this obviously doesn't work.  It seems like I'm not even in the ballpark.

    Could someone please point me in the right direction?

    Thanks!

    Friday, April 21, 2017 6:40 PM

Answers

  • Hi MattP34,

    I try to find the documentation regarding System.Windows.FrameworkElement.ContextMenuOpeningEvent

    it looks like it is only supported for Windows forms and will not work in MS Office.

    I try to find the similar event for MS Office but not get anything useful.

    I find the Before Right click event. that you can try to use.

    because it will occurs when the editing area of the document window is right-clicked.

    so it may solve your issue.

    private void DocumentBeforeRightClick()
    {
        Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
        vstoDoc.BeforeRightClick += new Microsoft.Office.Tools.Word.ClickEventHandler(ThisDocument_BeforeRightClick);
    }
    
    void ThisDocument_BeforeRightClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
    {
        Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
        System.Windows.Forms.MessageBox.Show(vstoDoc.Name + " was right-clicked.");
        e.Cancel = true;
    }
    

    Reference:

    Document.BeforeRightClick Event

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MattP34 Monday, April 24, 2017 8:45 PM
    Monday, April 24, 2017 7:54 AM
    Moderator

All replies

  • I think I'm closer now. I'm using this:

    System.Windows.Controls.ContextMenuEventHandler c = new System.Windows.Controls.ContextMenuEventHandler(Wd.GetMenuName);
    System.Windows.FrameworkElement myFE = new System.Windows.FrameworkElement();
    myFE.AddHandler(System.Windows.FrameworkElement.ContextMenuOpeningEvent, c, true)

    However, the event still isn't firing. Any suggestions?  My event handler is only supposed to print "hello" to a message box:

    public static void GetMenuName(object o, ContextMenuEventArgs e)

    { MessageBox.Show("Hello"); }

    Thanks!

    Friday, April 21, 2017 9:31 PM
  • Hi MattP34,

    I try to find the documentation regarding System.Windows.FrameworkElement.ContextMenuOpeningEvent

    it looks like it is only supported for Windows forms and will not work in MS Office.

    I try to find the similar event for MS Office but not get anything useful.

    I find the Before Right click event. that you can try to use.

    because it will occurs when the editing area of the document window is right-clicked.

    so it may solve your issue.

    private void DocumentBeforeRightClick()
    {
        Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
        vstoDoc.BeforeRightClick += new Microsoft.Office.Tools.Word.ClickEventHandler(ThisDocument_BeforeRightClick);
    }
    
    void ThisDocument_BeforeRightClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
    {
        Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
        System.Windows.Forms.MessageBox.Show(vstoDoc.Name + " was right-clicked.");
        e.Cancel = true;
    }
    

    Reference:

    Document.BeforeRightClick Event

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MattP34 Monday, April 24, 2017 8:45 PM
    Monday, April 24, 2017 7:54 AM
    Moderator
  • Thanks Deepak,

    The right click event that you supplied is a good way to trigger a method call when opening a context menu.

    Monday, April 24, 2017 8:45 PM