none
How to make a ContextMenu for Word2010 using VSTO RRS feed

  • Question

  • Hi!

    I have a question about ContextMenu. In the usual VB Windows form application, it is easy to make a ContextMenu like following.

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim cmenu As ContextMenu = New ContextMenu()
            cmenu.MenuItems.Add("Insert")
            cmenu.MenuItems.Add("Delete")
            cmenu.MenuItems.Add("Duplicate")
            AddHandler cmenu.MenuItems(0).Click, AddressOf Me.insertClick
            AddHandler cmenu.MenuItems(1).Click, AddressOf Me.deleteClick
            AddHandler cmenu.MenuItems(2).Click, AddressOf Me.duplicateClick
            cmenu.Show(sender, New System.Drawing.Point(0, 0))
        End Sub

        Private Sub insertClick(ByVal sender As Object, ByVal e As System.EventArgs)
            MessageBox.Show("Insert clicked!")
        End Sub

        Private Sub deleteClick(ByVal sender As Object, ByVal e As System.EventArgs)
            MessageBox.Show("Delete clicked!")
        End Sub

        Private Sub duplicateClick(ByVal sender As Object, ByVal e As System.EventArgs)
            MessageBox.Show("Duplicate clicked!")
        End Sub
    End Class

    However if I want to make ContextMenu when the button of the customized Word ribbon has pushed, I cannot get the control (System.Windows.Forms.Control) as the first parameter of the ContextMenu Show method.

    Sub SomeButtonPushed(ByVal control As Office.IRibbonControl)
      ...

      Dim cmenu As ContextMenu = New ContextMenu()
      cmenu.MenuItems.Add("a")
      cmenu.MenuItems.Add("b")
      cmenu.Show(control, pos:=New System.Drawing.Point(0, 0))

    End Sub

    This code reports the System.InvalidCastException. System.__ComObject cannot be casted as System.Windows.Forms.Control.

    Does anyone has solutions for this issue?

    Thanks,

    Wednesday, October 5, 2011 4:32 AM

Answers

  • Hi tmakita

    You cannot use Windows Forms context menus in an Office context. Office has its own Context Menus, which in 2010 are part of the Ribbon UI.

    You mention VSTO in the subject line of your message and you mention a Ribbon customization, so I take it you are using VSTO. But is this a VSTO Add-in or a VSTO document?

    See this article to get you started:
    http://msdn.microsoft.com/en-us/library/ee691832.aspx


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by tmakita Wednesday, October 5, 2011 1:01 PM
    Wednesday, October 5, 2011 10:26 AM
    Moderator

All replies

  • Hi tmakita

    You cannot use Windows Forms context menus in an Office context. Office has its own Context Menus, which in 2010 are part of the Ribbon UI.

    You mention VSTO in the subject line of your message and you mention a Ribbon customization, so I take it you are using VSTO. But is this a VSTO Add-in or a VSTO document?

    See this article to get you started:
    http://msdn.microsoft.com/en-us/library/ee691832.aspx


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by tmakita Wednesday, October 5, 2011 1:01 PM
    Wednesday, October 5, 2011 10:26 AM
    Moderator
  • Hi Cindy,

    Thank you for your reply.

    If you click the suggested link and see the "Community post", there is *MY POST* in June this year. Yes, I found this page in the past but have been forgotten until today.

    I will test the sample code written in this page. Anyway I appreciate your help.

    Thanks,

    Wednesday, October 5, 2011 11:02 AM