Create menu item programmatically

285 views
Skip to first unread message

Chezky

unread,
Aug 18, 2011, 8:27:54 AM8/18/11
to Excel-DNA
Hi,

I am able to designate menu items by using the ExcelCommand(MenuText)
annotation of my method. Is there a way to create menu items that map
to methods programmatically, at AutoOpen or later?

Thanks,
Chezky

Govert van Drimmelen

unread,
Aug 18, 2011, 9:49:24 AM8/18/11
to Excel-DNA
Hi Chezky,

Yes. Check the Distribution\Samples\Ribbon\CommandBarsAllWays.dna
sample.

The sample shows how to do extended command bars in three ways:
1. Add some xml in the .dna files, similar to the ribbon customization
(it can even be mixed with the ribbon xml).
2. Call ExcelCommandBarUtil.LoadCommandBars( ...) with a similar .xml
string.
3. Call the Command Bar wrappers from AutoOpen. These call the COM
interfaces late-bound. This is what you are asking for in your
question.

Regards,
Govert

Chezky

unread,
Aug 19, 2011, 12:00:49 AM8/19/11
to Excel-DNA
some more qeustions:
how can I add a button directly without a higher level menu? Do I have
to access a different command bar? Is there a good source documenting
the commandbars I can access, and their names? I feel a little like
i'm in the cockpit with this code, but i'm flying blind...:)
Can I use this kind of code to add a tab to the ribbon, or to check
for an existing tab to add my controls to?

Thanks!
Chezky

Chezky

unread,
Aug 24, 2011, 10:09:34 PM8/24/11
to Excel-DNA
I have added elements to the Ribbon addin menus, but I get an error in
Excel stating that it cannot find my macro. I'm setting the onAction
member to my function, with a parameter. Are there any special
settings I have to watch out for to get this wired right?

Thanks,
Chezky

On Aug 19, 12:00 am, Chezky <chezkyr...@gmail.com> wrote:
> some more qeustions:
> how can I add a button directly without a higher level menu? Do I have
> to access a different command bar? Is there a good source documenting
> the commandbars I can access, and their names? I feel a little like
> i'm in the cockpit with this code, but i'm flying blind...:)
> Can I use this kind of code to add a tab to theribbon, or to check
> for an existing tab to add my controls to?
>
> Thanks!
> Chezky
>
> On Aug 18, 9:49 am, Govert van Drimmelen <gov...@icon.co.za> wrote:
>
>
>
>
>
>
>
> > Hi Chezky,
>
> > Yes. Check the Distribution\Samples\Ribbon\CommandBarsAllWays.dna
> > sample.
>
> > The sample shows how to do extended command bars in three ways:
> > 1. Add some xml in the .dna files, similar to theribboncustomization
> > (it can even be mixed with theribbonxml).
> > 2. Call ExcelCommandBarUtil.LoadCommandBars( ...) with a similar .xml
> > string.
> > 3. Call the Command Bar wrappers from AutoOpen. These call the COM
> > interfaces late-bound. This is what you are asking for in your
> > question.
>
> > Regards,
> > Govert
>
> > On Aug 18, 2:27 pm, Chezky <chezkyr...@gmail.com> wrote:
>
> > > Hi,
>
> > > I am able to designate menu items by using the ExcelCommand(MenuText)
> > > annotation of my method. Is there a way to create menu items that map
> > > to methodsprogrammatically, at AutoOpen or later?
>
> > > Thanks,
> > > Chezky

Govert van Drimmelen

unread,
Aug 25, 2011, 2:33:54 AM8/25/11
to Excel-DNA
Hi Chezky,

There are different ways to wire up the actions.
1. Add an instance member to your ExcelRibbon-derived class, and use
the name in the onAction attribute of the ribbon. A common issue is
that you have to make sure that the type or assembly is
ComVisible(true).
2. Add a public static member as a macro, and hook it up via the pair
onAction="RunTagMacro" tag="DoMyStuff". Your macro won't take any
parameters in this case.

Are you able to run the samples?
Maybe you can work from there to see where it might be going wrong.

-Govert

Chezky

unread,
Sep 5, 2011, 11:11:09 PM9/5/11
to Excel-DNA
when I add my method to the ribbon using the ExcelCommand directive,
is there any way to control in what position in the ribbon this menu
and its item get added? Right now, it's always first, but I'd like to
make it last, after the ones I create programmatically.

Thanks,
Chezky

On Aug 25, 2:33 am, Govert van Drimmelen <gov...@icon.co.za> wrote:
> Hi Chezky,
>
> There are different ways to wire up the actions.
> 1. Add an instance member to your ExcelRibbon-derived class, and use
> the name in the onAction attribute of theribbon. A common issue is
> that you have to make sure that the type or assembly is
> ComVisible(true).
> 2. Add a public static member as a macro, and hook it up via the pair
> onAction="RunTagMacro" tag="DoMyStuff". Your macro won't take any
> parameters in this case.
>
> Are you able to run the samples?
> Maybe you can work from there to see where it might be going wrong.
>
> -Govert
>
> On Aug 25, 4:09 am, Chezky <chezkyr...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I have added elements to theRibbonaddin menus, but I get an error in
> > Excel stating that it cannot find my macro. I'm setting the onAction
> > member to my function, with a parameter. Are there any special
> > settings I have to watch out for to get this wired right?
>
> > Thanks,
> > Chezky
>
> > On Aug 19, 12:00 am, Chezky <chezkyr...@gmail.com> wrote:
>
> > > some more qeustions:
> > > how can I add a button directly without a higher levelmenu? Do I have
> > > to access a different command bar? Is there a good source documenting
> > > the commandbars I can access, and their names? I feel a little like
> > > i'm in the cockpit with this code, but i'm flying blind...:)
> > > Can I use this kind of code to add a tab to theribbon, or to check
> > > for an existing tab to add my controls to?
>
> > > Thanks!
> > > Chezky
>
> > > On Aug 18, 9:49 am, Govert van Drimmelen <gov...@icon.co.za> wrote:
>
> > > > Hi Chezky,
>
> > > > Yes. Check the Distribution\Samples\Ribbon\CommandBarsAllWays.dna
> > > > sample.
>
> > > > The sample shows how to do extended command bars in three ways:
> > > > 1. Add some xml in the .dna files, similar to theribboncustomization
> > > > (it can even be mixed with theribbonxml).
> > > > 2. Call ExcelCommandBarUtil.LoadCommandBars( ...) with a similar .xml
> > > > string.
> > > > 3. Call the Command Bar wrappers from AutoOpen. These call the COM
> > > > interfaces late-bound. This is what you are asking for in your
> > > > question.
>
> > > > Regards,
> > > > Govert
>
> > > > On Aug 18, 2:27 pm, Chezky <chezkyr...@gmail.com> wrote:
>
> > > > > Hi,
>
> > > > > I am able to designatemenuitems by using the ExcelCommand(MenuText)
> > > > > annotation of my method. Is there a way to createmenuitems that map

Chezky

unread,
Sep 11, 2011, 5:50:31 PM9/11/11
to Excel-DNA
I'm able to create the ribbon using an ExcelRibbon-derived class,
which is ComVisible. I set the onAction to an instance member method
of this class. The ribbon appears, but it doesn't trigger my method.
I'm not sure how to debug further.
I'm compiling my C# code into a dll, and my dna file has
ComServer="true" and Pack="true", don't know if this changes anything.

Thanks,
Chezky

On Aug 25, 2:33 am, Govert van Drimmelen <gov...@icon.co.za> wrote:
> HiChezky,
>
> There are different ways to wire up the actions.
> 1. Add an instance member to your ExcelRibbon-derived class, and use
> the name in the onAction attribute of the ribbon. A common issue is
> that you have to make sure that the type or assembly is
> ComVisible(true).
> 2. Add a public static member as a macro, and hook it up via the pair
> onAction="RunTagMacro" tag="DoMyStuff". Your macro won't take any
> parameters in this case.
>
> Are you able to run the samples?
> Maybe you can work from there to see where it might be going wrong.
>
> -Govert
>
> On Aug 25, 4:09 am,Chezky<chezkyr...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I have added elements to the Ribbon addin menus, but I get an error in
> > Excel stating that it cannot find my macro. I'm setting the onAction
> > member to my function, with a parameter. Are there any special
> > settings I have to watch out for to get this wired right?
>
> > Thanks,
> >Chezky
>
> > On Aug 19, 12:00 am,Chezky<chezkyr...@gmail.com> wrote:
>
> > > some more qeustions:
> > > how can I add a button directly without a higher level menu? Do I have
> > > to access a different command bar? Is there a good source documenting
> > > the commandbars I can access, and their names? I feel a little like
> > > i'm in the cockpit with this code, but i'm flying blind...:)
> > > Can I use this kind of code to add a tab to theribbon, or to check
> > > for an existing tab to add my controls to?
>
> > > Thanks!
> > >Chezky
>
> > > On Aug 18, 9:49 am, Govert van Drimmelen <gov...@icon.co.za> wrote:
>
> > > > HiChezky,
>
> > > > Yes. Check the Distribution\Samples\Ribbon\CommandBarsAllWays.dna
> > > > sample.
>
> > > > The sample shows how to do extended command bars in three ways:
> > > > 1. Add some xml in the .dna files, similar to theribboncustomization
> > > > (it can even be mixed with theribbonxml).
> > > > 2. Call ExcelCommandBarUtil.LoadCommandBars( ...) with a similar .xml
> > > > string.
> > > > 3. Call the Command Bar wrappers from AutoOpen. These call the COM
> > > > interfaces late-bound. This is what you are asking for in your
> > > > question.
>
> > > > Regards,
> > > > Govert
>

Govert van Drimmelen

unread,
Sep 12, 2011, 3:42:10 AM9/12/11
to Excel-DNA
Hi Chezky,

Which of the two options from my previous reply are your trying:
1. onAction="RunTagMacro" tag="MyMacro" and then a public static
method, or
2. onAction="MyRibbonMacro" and then a public instance method with the
right signature?

It might be easiest it you start with one of the samples in the
\Distribution\Samples\Ribbon and when that is working, try to
customize further.

Regards,
Govert
Reply all
Reply to author
Forward
0 new messages