How can I access the methods written in Excel Add-in WPF?

Aug 25, 2014 at 12:16 PM
Edited Aug 25, 2014 at 12:21 PM
Hi,

I have created an excel Add-In using Excel-DNA, WPF, C#. In the ribbon a button is added to generate the pdf for the opened excel and this code is working fine.

Now in my another standalone c# application I want to programmatically call the above Add-In (XLL) and call the methods "generatepdf" that will generate the PDF of excel opened.

I am trying to do something like this
((Microsoft.Office.Interop.Excel.Application)this.excelInvoker.ExcelApplication).RegisterXLL(AddinPath);

 Microsoft.Office.Core.COMAddIns myComAddIns = excel.COMAddIns;
                       
 COMAddIn AddIn = null;

 foreach (COMAddIn myComAddIn in myComAddIns)
                        {
                            string desc = myComAddIn.Description;
                                                    if (desc == "AddInForExcel")
                            {
                                AddIn = myComAddIn;
                                break;
                            }                                    
               }
  object comAddinObj = AddIn .Object;
Issues are:
1) AddIn.Object is coming null
2) How can I call the methods of excel Add-In like "generatepdf"?

Please help me.
Coordinator
Aug 25, 2014 at 12:47 PM
Hi,

The .xll add-in is not a COM Add-In, so won't be in the ComAddIns collection (though the ribbon helper might be found there). You'd have to set the ComAddIn.Object explicitly in your own code for the hook-up to work. See the discussion here: https://exceldna.codeplex.com/discussions/405224

If you've created the "GeneratePdf" method as a macro (as a public static void method) you can call it directly using Application.Run("GeneratePdf"). That should be much easier.

-Govert
Aug 25, 2014 at 1:11 PM
Trying out the second option my Add-In code is
[ComVisible(true)]
public class MyRibbon : ExcelRibbon       
{    
    //[ExcelCommand]
    public void OnRefreshData(IRibbonControl control)
    {
           }

    public static void GeneratePdf()
    {
//pdf generation logic
    }
  
}
and C# application my code is
((Microsoft.Office.Interop.Excel.Application)this.excelInvoker.ExcelApplication).RegisterXLL(AddinPath);

((Microsoft.Office.Interop.Excel.Application)this.excelInvoker.ExcelApplication).Run("GeneratePdf");
Still no PDF is generated. Am I doing something wrong?
Aug 25, 2014 at 1:48 PM
Govert,

The second way is working. Thanks for the help.

((Microsoft.Office.Interop.Excel.Application)this.excelInvoker.ExcelApplication).Run("GeneratePdf");