Re: C# and (hopefully) WPF Form

663 views
Skip to first unread message

Govert van Drimmelen

unread,
Jul 30, 2012, 5:16:44 PM7/30/12
to Excel-DNA
Hi Paul,

For your ribbon to call code in your ExcelRibbon-derived class you
need to add some (non-static) methods to the class, and update the
ribbon .xml.

Something like this will work with the .cml you've shown:


[ComVisible(true)]
public class Class1 : ExcelRibbon
{
public void OnButtonPressed(IRibbonControl control)
{
FormRegistration fr = new FormRegistration();
fr.ShowDialog();
}
}



Regards,
Govert


On Jul 30, 6:44 pm, Paul Menefee <paul.mene...@gmail.com> wrote:
> UPDATE:  I was able to open the WPF form via a function, but this is not
> the desired outcome.  It seems I do not have access to my library from the
> ribbon but do from functions.
> namespace myLibrary
> {
>     [ComVisible(true)]
>     public class Class1 : ExcelRibbon
>     {
>         [ExcelFunction(Description = "Regstration Number", Category =
> "Useful functions")]
>         public static void testpopup()
>         {
>             FormRegistration fr = new FormRegistration();
>             fr.ShowDialog();
>         }
>    }
>
> }
>
> <DnaLibrary Name="myLibrary" Language="C#">
>   <ExternalLibrary Path="myLibrary.dll" />
>  <CustomUI>
>     <!-- Inside here is the RibbonX xml passed to Excel -->
>     <!-- This can also be loaded by Excel 2007 because of the namespace -->
>     <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'
> loadImage='LoadImage'>
>       <ribbon>
>         <tabs>
>           <tab id='CustomTab' label='My Test'>
>             <group id='SampleGroup' label='Sample Group'>
>               <button id='Button1' label='Button Label' image='M'
> size='large' onAction='RunTagMacro' tag='ShowHelloMessage' />
>               <button id='Button2' label='Second Button' image='M'
> size='normal' onAction='OnButtonPressed'/>
>               <button id='Button3' label='Register' image='M' size='normal'
> onAction='CallbackOnAction'/>
>             </group >
>           </tab>
>         </tabs>
>       </ribbon>
>     </customUI>
>   </CustomUI>
> </DnaLibrary>
>
>
>
>
>
>
>
> On Monday, July 30, 2012 11:50:44 AM UTC-4, Paul Menefee wrote:
>
> > I'd like used Excel-DNA to create a custom ribbon tab and added functions
> > and buttons.  I would now like to have the user enter settings for the
> > add-in.  Things such as parameters the functions will use for its
> > calculations and basic user information.  I realize that I could enter this
> > data in a spreadsheet but I'd rather not if possible.

Stefan Kemp

unread,
Feb 6, 2014, 7:33:23 AM2/6/14
to exce...@googlegroups.com
Hi Govert,

I have a similar situation and have tried your solution but still nothing happens when I click the custom button on my Ribbon. Here is what I have in my dna file (sorry I don't know how to markup code):

<DnaLibrary Name="First Add-In" RuntimeVersion="v4.0" Language="C#">
  <ExternalLibrary Path="MyLibrary.dll" Pack="true"/> 
  <Image Name="M" Path="M.png" Pack="true" />
  <CustomUI>
    <customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui' loadImage='LoadImage'>
      <ribbon>
        <tabs>
          <tab id='CustomTab' label='My 2010 Tab'>
            <group id='SampleGroup' label='My Sample Group'>
              <button id='Button1' label='My Second Button' image='M' size='normal' onAction='RunTagMacro' tag='ReformatSelection='/>

            </group >
          </tab>
        </tabs>
      </ribbon>
    </customUI>
  </CustomUI> 
</DnaLibrary>

And in my class library I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExcelDna.Integration;
using System.Runtime.InteropServices;
using ExcelDna.Integration.CustomUI;
using System.Windows.Forms;

namespace MyLibrary
{

    [ComVisible(true)]
    public class Class1 : ExcelRibbon
    {               
       
        public void ReformatSelection(IRibbonControl control1)
        {
            MessageBox.Show("Hello");

Govert van Drimmelen

unread,
Feb 6, 2014, 7:41:37 AM2/6/14
to exce...@googlegroups.com
Hi Stefan,

Can you try:

   <button id='Button1' label='My Second Button' image='M' size='normal' onAction='ReformatSelection'/>

The RunTagMacro and matching Tag story is only needed if you want to run a regular Excel macro from the ribbon button.
Such a macro would be defined in your code as a static mathod taking no parameters:
    public static void ReformatSelection()
and then youd leave the xml markup as you had it.

but using the regular Ribbon callback is mostly better.

-Govert

Stefan Kemp

unread,
Feb 6, 2014, 9:27:37 AM2/6/14
to exce...@googlegroups.com
Thanks Govert. Using your suggested xml markup worked (the callback method must not be static and iRibbonControl parameter is required). Using RunTagMacro also worked with static method and no parameter.
Reply all
Reply to author
Forward
0 new messages