Pregunta

I am testing Excel-Dna and want to a button in Excel that runs some simple code. As I understood it the following code should add a button in the Add-Ins tab in Excel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExcelDna.Integration.CustomUI;
using NetOffice;
using NetOffice.ExcelApi;
using ExcelDna.Integration;
using ExcelPluginTest;

namespace ExcelPluginTest
{
    public class AddIn : IExcelAddIn
    {
        public static Application Excel { get; set; }        
        public void AutoOpen()
        {
            Factory.Initialize();
            Excel = new Application(null, ExcelDnaUtil.Application);

        }

        public void AutoClose()
        {           
        }
    }

    public class ExcelHelper
    {
        public static Application Excel { get { return AddIn.Excel; } }

        [ExcelCommand(MenuName = "ExcelPlugin Test", MenuText = "Write the Excel version")]
        public void WriteTheVersion()
        {
            var ver = Excel.Version;
            var rng = Excel.Range("B3");
            rng.Value = ver;
        }
    }
}

However the Add-Ins tab is not even visible even though it's checked in File | Options | Customize Ribbon. I am not sure if this happens because there's nothing to show (aka problem in my code) or that the button is actually there but I can't see it because the Add-Ins tab is hidden (I did confirm that the AddIn is loaded (set BP in AutoOpen)).

¿Fue útil?

Solución

For Excel-DNA to register your method as a macro with Excel, it needs to be 'static'. So this should work:

    [ExcelCommand(MenuName = "ExcelPlugin Test", MenuText = "Write the Excel version")]
    public static void WriteTheVersion()
    {
    }

Note that there is an Excel bug which affects the [ExcelCommand...] style menus under Excel 2013 - this is fixed in the upcoming Excel-DNA version 0.32.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top