Re: Context Menu From C#

1,477 views
Skip to first unread message

Govert van Drimmelen

unread,
Apr 10, 2013, 5:21:50 AM4/10/13
to Excel-DNA
Hi Kieran,

I think you'll be able to add context menus in a few ways - in the
Ribbon xml for Excel 2010+, via COM automation and the CommandBars
objects, or for older Excel with something like this (you'll have to
translate to C#):

Public Shared Sub MyExposedFunction()
MsgBox("Hello World")
End Sub

Dim myCommand() As Object = New Object() {"My Menu Item",
"MyExposedFunction"}
XlCall.Excel(xlfAddCommand, 7, 4, myCommand, 0)

* What version(s) of Excel are you targeting?
* Are you making a custom ribbon or Excel-DNA CommandBars xml?
* Do you know how to add context menu items in VBA?

Regards,
Govert


* * * * * * * * * * * * * * * * * * * * * * * * *
You can encourage future Excel-DNA development
by arranging a corporate support agreement
or making a donation to the project via PayPal.

See: http://excel-dna.net/support/
* * * * * * * * * * * * * * * * * * * * * * * * *

Govert van Drimmelen

unread,
Apr 10, 2013, 8:40:34 AM4/10/13
to Excel-DNA
Hi Kieran,

For Excel 2010+, the context menus can be customized in the ribbon
xml.
(Note that you need to update the xmlns namespace too.)

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/
customui">
<ribbon>
[.....]
</ribbon>
<contextMenus>
<contextMenu idMso="ContextMenuCell">

<button idMso="FileSave" insertBeforeMso="Cut" />

<button id="MyButton" label="Toggle Case Upper/Lower/Proper"
insertBeforeMso="Cut"
onAction="ToggleCaseMacro"
imageMso="HappyFace"/>

<menu id="MySubMenu" label="Case Menu" insertBeforeMso="Cut"
>
<button id="Menu1Button1" label="Upper Case"
imageMso="U" onAction="UpperMacro"/>
<button id="Menu1Button2" label="Lower Case"
imageMso="L" onAction="LowerMacro"/>
<button id="Menu1Button3" label="Proper Case"
imageMso="P" onAction="ProperMacro"/>
</menu>

<menuSeparator id="MySeparator" insertBeforeMso="Cut" />

</contextMenu>
</contextMenus>
</customUI>

-Govert

pooja venkateswaran

unread,
Jul 24, 2014, 3:12:39 AM7/24/14
to exce...@googlegroups.com
Hi Govert,

I need to implement context menu for all excel versions(2003,2007,2010,2013). Just as mentioned ribbon works for 2010+ and commandbar objects can be used for old versions.
On trying to implement command bars for 2010+, I get an error. Is it possible to implement both the ribbon as well as the commandBars in the code.

Thanks,
Pooja

On Wednesday, 10 April 2013 04:46:55 UTC-4, knaier wrote:
Hi guys,

I'm loving ExcelDNA and almost live on it in production now. 

However, i need to add a context menu via C# (we can't do it from the XLS/VBA layer as we have 100s of sheets).

Is there a way to do this?

Any help would be much appreciated. Users are very happy about loosing the right click functionality

Thanks

Kieran

Govert van Drimmelen

unread,
Jul 24, 2014, 4:27:40 AM7/24/14
to <exceldna@googlegroups.com>
Hi Pooja,

I'd expect the CommandBars to still work in new Excel versions.
What error do you get? Can you post a small self-contained example?

Otherwise you might need to use the Ribbon .xml to customize the Context menus on new versions, and fall back to the CommandBars code on older versions.

-Govert


--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

pooja venkateswaran

unread,
Jul 24, 2014, 4:38:14 AM7/24/14
to exce...@googlegroups.com
Hi Govert,

I added the below to my code

void startup()
{
AddContextMenu();
}

void AddContextMenu()
        {

            DefineShortcutMenu();
            _excelApplication.SheetBeforeRightClick +=
                new Microsoft.Office.Interop.Excel.AppEvents_SheetBeforeRightClickEventHandler
                    (Application_SheetBeforeRightClick);
            writeToText.Click +=
                new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
                    (writeToText_Click);
        }
        void writeToText_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
           
        }
        void Application_SheetBeforeRightClick(object Sh, Microsoft.Office.Interop.Excel.Range Target, ref bool Cancel)
        {
            selectedCells = Target;
        }
        private void DefineShortcutMenu()
        {

            Microsoft.Office.Core.MsoControlType menuItem = Microsoft.Office.Core.MsoControlType.msoControlButtonDropdown;
            _excelApplication.CommandBars["Cell"].Reset();
            object id = 1;
            object tag = null;
            object visible = 1;
            object recusive = false;
            writeToText = (Microsoft.Office.Core.CommandBarButton)_excelApplication.CommandBars["Cell"].
                Controls.Add(menuItem, Type.Missing, Type.Missing, 1, true);

            writeToText.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
            writeToText.Caption = "Write to a Text File";
            writeToText.Tag = "0";
        }
The error that I get reads "Value doesn't fall in the expected range.".

Thanks, Pooja

Govert van Drimmelen

unread,
Jul 24, 2014, 6:31:00 AM7/24/14
to exce...@googlegroups.com

Hi Pooja,

 

At which line do you get the error?

 

-Govert

pooja venkateswaran

unread,
Jul 24, 2014, 6:37:24 AM7/24/14
to exce...@googlegroups.com
Hi Govert,

I am getting an error in this line

 Microsoft.Office.Core.MsoControlType menuItem = Microsoft.Office.Core.MsoControlType.msoControlButtonDropdown;

When I change it to to just msoControlButton in the above line , it works correctly. What I am trying to do i create a dropdown with multiple buttons. The msoControlPopup also throws the same error. I am not sure what is right implementation for creating sub buttons to a popup/dropdown

-Pooja

Govert van Drimmelen

unread,
Jul 24, 2014, 6:58:20 AM7/24/14
to exce...@googlegroups.com

Hi Pooja,

 

Do I understand right that this works fine under Excel 2007, but not in Excel 2010+?

 

If so, I guess that feature is not supported in the newer Excel version. You might try to post to the Excel for Developers forum (http://social.msdn.microsoft.com/Forums/office/en-US/home?forum=exceldev) – maybe with a VBA version of the problematic code - since this is not really an Excel-DNA issue.

 

And you might have to take the Ribbon approach for context menus in the newer Excel versions.

Excel 2010 added support for making context menus with the ribbon xml, so I’d guess they did some work on the context menus, and not all features that worked before were fully implemented in the new version.

 

-Govert

--

Reply all
Reply to author
Forward
0 new messages