Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel-DNA — Templated CustomTaskPane Exception

I'm having the weirdest issue with CustomTaskPaneFactory in Excel-DNA 0.32.

CustomTaskPaneFactory throws an exception when I create a task pane with a class (MyIntCTP) that inherits from a templated task pane (MyCTP<T>).

Here is a example that illustrates the context:

<DnaLibrary RuntimeVersion="v4.0" Language="CS">
<Reference Path="System.Windows.Forms.dll" />
<![CDATA[
    using ExcelDna.Integration;
    using ExcelDna.Integration.CustomUI;
    using System.Windows.Forms;

    internal class ThisAddIn : IExcelAddIn
    {
        public void AutoOpen()
        {
            var p = CustomTaskPaneFactory.CreateCustomTaskPane(typeof(MyIntCTP), "o");
            p.Visible = true;
        }
        public void AutoClose()
        { }
    }

    public class MyCTP<T> : UserControl
    { }
    public class MyIntCTP : MyCTP<int>
    { }
]]>
</DnaLibrary>

The above code doesn't work. Excel-DNA crashes on CreateCustomTaskPane with the following System.Runtime.InteropServices.COMException:

Unable to create specified ActiveX control
   at ExcelDna.Integration.CustomUI.ICTPFactory.CreateCTP(String CTPAxID, String CTPTitle, Object CTPParentWindow)
   at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(String controlProgId, String title, Object parent)
   at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Type userControlType, String title, Object parent)
   at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Type userControlType, String title)
   [...]

However it does work if the parent task pane (MyCTP) implements an interface, as so (note that all classes are marked as public):

public interface DummyInterface
{ }
public class MyCTP<T> : UserControl, DummyInterface
{ }

I don't need the interface, but I want to keep the template. Any ideas?

like image 579
lelimacon Avatar asked Feb 24 '26 12:02

lelimacon


1 Answers

I have faced the same problem.

The solution is the com visibility which will then pass the COM(Active X) object.

   [ComVisible(true)]
   public class MyIntCTP : MyCTP<int>
   { }

Try the following links, they will give you an overview, that will help. https://msdn.microsoft.com/en-us/library/aa942861.aspx

Have a look at the git link given below to help with project. https://github.com/KevinT/ExcelDna/blob/master/Distribution/Samples/CustomTaskPane.dna

like image 166
prakhar srivastava Avatar answered Feb 26 '26 01:02

prakhar srivastava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!