Re: ArgumentNullException error using CustomTaskPaneFactory

212 views
Skip to first unread message

Govert van Drimmelen

unread,
Mar 12, 2013, 9:05:30 AM3/12/13
to Excel-DNA
Hi Brett,

It seems your call to Type.GetType("MyUserControl1") is failing.

If the type is defined in a Namespace (say MyAddIn), you probably need
to say:
Type.GetType("MyAddIn.MyUserControl1") or something.

-Govert



On Mar 12, 4:31 am, Brett Olson <brettol...@cox.net> wrote:
> The sample CustomTaskPanevb.dna works fine as-is. I get the Ribbon with the
> buttons and the Task Pane is created as expected.
>
> However when I import the sample code into Visual Studio and create a
> UserControl inside Visual Studio I get the following error on the hilited
> line: (I get the Ribbon with the buttons, but the when I click to show the
> Task Pane it fails)
>
> Any Suggestions?
>
> Thanks,
>
> Brett
>
> Public Sub OnShowCTP(ByVal control As IRibbonControl)
>
> If ctp Is Nothing Then
>
> ctp =
> CustomTaskPaneFactory.CreateCustomTaskPane(Type.GetType("MyUserControl1"),
> "My Super Custom Task Pane!")
>
> ctp.Visible = True
>
> ctp.DockPosition = MsoCTPDockPosition.msoCTPDockPositionLeft
>
> AddHandler ctp.DockPositionStateChange, AddressOf
> ctp_DockPositionStateChange
>
> AddHandler ctp.VisibleStateChange, AddressOf ctp_VisibleStateChange
>
> Else
>
> ctp.Visible = True
>
> End If
>
> End Sub
>
> *System.ArgumentNullException was unhandled by user code*
> *  HResult=-2147467261*
> *  Message=Value cannot be null.*
> *Parameter name: type*
> *  ParamName=type*
> *  Source=mscorlib*
> *  StackTrace:*
> *       at System.Activator.CreateInstance(Type type, Boolean nonPublic)*
> *       at System.Activator.CreateInstance(Type type)*
> *       at
> ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Ty pe
> userControlType, String title, Object parent)*
> *       at
> ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Ty pe
> userControlType, String title)*
> *       at Brett01.MyRibbon.OnShowCTP(IRibbonControl control) in
> C:\Users\BOlson\Documents\Visual Studio
> 2012\Projects\Brett01\Brett01\myRibbon.vb:line 30*
> *  InnerException:*

Govert van Drimmelen

unread,
Mar 12, 2013, 10:00:25 AM3/12/13
to Excel-DNA
Hi Brett,

You don't have to create an instance of MyUserControl1, you just need
to pass in a Type.

I think your call to Type.GetType("MyUserControl1") failed and
returned null. The VS project might have made a namespace. I think you
can also say it as

Dim theType As Type
theType = GetType(MyUserControl1)
ctp = CustomTaskPaneFactory.CreateCustomTaskPane(theType, "My Super
Custom Task Pane!")

And in this case you should get a compile-time error if there is a
problem.

-Govert


On Mar 12, 3:29 pm, Brett Olson <brettol...@cox.net> wrote:
> Thanks Govert
>
> Im not using an addin, i used Visual Studio to create the simple user control which is in the VS ProjectThe class name is MyUserControl1.
>
> Do i need to create a variable of MyUserControl1 and then ge thevariable type?
>
> Thanks, Brett
>
>
>
>
>
>
>
> On Monday, March 11, 2013 7:31:41 PM UTC-7, Brett Olson wrote:
> > The sample CustomTaskPanevb.dna works fine as-is. I get the Ribbon with the buttons and the Task Pane is created as expected.
>
> > However when I import the sample code into Visual Studio and create a UserControl inside Visual Studio I get the following error on the hilited line: (I get the Ribbon with the buttons, but the when I click to show the Task Pane it fails)
>
> > Any Suggestions?
>
> > Thanks,
>
> > Brett
>
> >     Public Sub OnShowCTP(ByVal control As IRibbonControl)
> >         If ctp Is Nothing Then
> >             ctp = CustomTaskPaneFactory.CreateCustomTaskPane(Type.GetType("MyUserControl1"), "My Super Custom Task Pane!")            ctp.Visible = True            ctp.DockPosition = MsoCTPDockPosition.msoCTPDockPositionLeft            AddHandler ctp.DockPositionStateChange, AddressOf ctp_DockPositionStateChange            AddHandler ctp.VisibleStateChange, AddressOf ctp_VisibleStateChange        Else
> >             ctp.Visible = True        End If    End Sub
>
> > System.ArgumentNullException was unhandled by user code
> >   HResult=-2147467261
> >   Message=Value cannot be null.
> > Parameter name: type
> >   ParamName=type
> >   Source=mscorlib
> >   StackTrace:
> >        at System.Activator.CreateInstance(Type type, Boolean nonPublic)
> >        at System.Activator.CreateInstance(Type type)
> >        at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Ty pe userControlType, String title, Object parent)
> >        at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Ty pe userControlType, String title)
> >        at Brett01.MyRibbon.OnShowCTP(IRibbonControl control) in C:\Users\BOlson\Documents\Visual Studio 2012\Projects\Brett01\Brett01\myRibbon.vb:line 30
> >   InnerException:

Govert van Drimmelen

unread,
Mar 12, 2013, 11:17:23 AM3/12/13
to Excel-DNA
Thank for update Brett - glad it's working now.

To be clear, the "Brett01" in Type.GetType("Brett01.MyUserControl1")
is the root namespace for your project, and not the name of the .xll
(though I guess these are the same in your case).

-Govert


On Mar 12, 5:07 pm, Brett Olson <brettol...@cox.net> wrote:
> To all noobs like me that are just starting, here are the changes I needed
> to make to the sample CustomTaskPaneVB.net to get it working inside VB.NET
> rather than the standalone .dna file.
>
> Three changes needed to be made.
>
>    - As the .dna file suggests, I had to add the [ComVisible(true)]
>    attribute to the, BUT... VB attributes use the '<' instead of '[', so the I
>    added the following just above the class in myRibbon.vb
>
> Imports System.Runtime.InteropServices
> <ComVisible(True)> _
>
>    - The following line caused an ArgumentNullException error when I used
>    the .dna code
>
> *(From .dna file)*      ctp =
> CustomTaskPaneFactory.CreateCustomTaskPane(Type.GetType("MyUserControl"),
> "Hello")
>
> *(Changed to)  *       ctp =
> CustomTaskPaneFactory.CreateCustomTaskPane(Type.GetType("Brett01.MyUserCont rol1"),
> "Hello") 'Brett01 is the name of my .XLL file and must be added!
>
>    -  As the .dna file suggests, I had to add the [ComVisible(true)]
>    attribute to the User Control Class, BUT... VB attributes use the '<'
>    instead of '['
>
> You can add this attribute in two different places, and it seems to in
> either location (otherwise you'll receive a COM error). Add this attribute
> either to the MyUserControl1.Designer.vb file, or add it to the
> MyUserControl1.resx file
>
> Imports System.Runtime.InteropServices
> <ComVisible(True)> _
>
> Thanks for help Govert. Hopefully, this helps the next person that is just
> getting started.
> > *System.ArgumentNullException was unhandled by user code*
> > *  HResult=-2147467261*
> > *  Message=Value cannot be null.*
> > *Parameter name: type*
> > *  ParamName=type*
> > *  Source=mscorlib*
> > *  StackTrace:*
> > *       at System.Activator.CreateInstance(Type type, Boolean nonPublic)*
> > *       at System.Activator.CreateInstance(Type type)*
> > *       at
> > ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Ty pe
> > userControlType, String title, Object parent)*
> > *       at
> > ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Ty pe
> > userControlType, String title)*
> > *       at Brett01.MyRibbon.OnShowCTP(IRibbonControl control) in
> > C:\Users\BOlson\Documents\Visual Studio
> > 2012\Projects\Brett01\Brett01\myRibbon.vb:line 30*
> > *  InnerException:*
Reply all
Reply to author
Forward
0 new messages