Ribbon not shown

667 views
Skip to first unread message

Karsten

unread,
Dec 8, 2011, 12:19:37 PM12/8/11
to Excel-DNA
Hi and hello everybody,
I just started to try ExcelDNA and got stuck when using ExcelRibbon. I
more or less copied the code from the CustomTaskPane.dna sample and
put it into a ExternalLibrary. While CustomTaskPane.dna works without
problems, XL2MemStub.dna does not show the ribbon. I pasted the code
below (the UIDataBrowser-Control is a simple UserControl that resides
in another source module).
The "Hypetenouse"-function is only there for test purposes - it gets
called without problems.

Second problem: I cannot debug into the ExcelDNA sources. I added
everything to my solution - even EXCEL.EXE - switched do .net fx 2.0.
When I set a breakpoint on the Hypotenuse function, the execution
stops but the stack trace says "external code" only. I would have
expected some ExcelDNA-code calling it.

BTW, I'm using Visual Studio 2010 Professional.

Anyone any ideas?


XL2MemStub.dna:

<DnaLibrary Language="C#" >
<ExternalLibrary Path="..\XL2Mem\XL2Mem\bin\Debug\XL2Mem.dll" />
</DnaLibrary>

UIManagement.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;

namespace insite.XL2Mem
{
public class XL2MemRibbon : ExcelRibbon
{
public void OnShowCTP(IRibbonControl control)
{
CTPManager.ShowCTP();
}

public void OnDeleteCTP(IRibbonControl control)
{
CTPManager.DeleteCTP();
}

public override string GetCustomUI(string uiName)
{
return
@"
<customUI xmlns='http://schemas.microsoft.com/
office/2006/01/customui' loadImage='LoadImage'>
<ribbon>
<tabs>
<tab id='CustomTab' label='Custom Task Pane Test'>
<group id='SampleGroup' label='CTP Control'>
<button id='Button1' label='Show CTP'
image='M' size='large' onAction='OnShowCTP' />
<button id='Button2' label='Delete CTP'
image='M' size='large' onAction='OnDeleteCTP' />
</group >
</tab>
</tabs>
</ribbon>
</customUI>
";
}
}

/////////////// Helper class to manage
CTP ///////////////////////////
public static class CTPManager
{
static CustomTaskPane ctp;

public static void ShowCTP()
{
if (ctp == null)
{
// Make a new one using
ExcelDna.Integration.CustomUI.CustomTaskPaneFactory
ctp =
CustomTaskPaneFactory.CreateCustomTaskPane(typeof(UIDataBrowser),
"Browse data");
ctp.Visible = true;
ctp.DockPosition =
MsoCTPDockPosition.msoCTPDockPositionLeft;
ctp.DockPositionStateChange +=
ctp_DockPositionStateChange;
ctp.VisibleStateChange += ctp_VisibleStateChange;
}
else
{
// Just show it again
ctp.Visible = true;
}
}


public static void DeleteCTP()
{
if (ctp != null)
{
// Could hide instead, by calling ctp.Visible = false;
ctp.Delete();
ctp = null;
}
}

static void ctp_VisibleStateChange(CustomTaskPane
CustomTaskPaneInst)
{
MessageBox.Show("Visibility changed to " +
CustomTaskPaneInst.Visible);
}

static void ctp_DockPositionStateChange(CustomTaskPane
CustomTaskPaneInst)
{
((UIDataBrowser)ctp.ContentControl).Action = "Moved to " +
CustomTaskPaneInst.DockPosition.ToString();
}
}

public class UDFs
{
[ExcelFunction(Description = "Calculates the length of the
hypotenuse in a right-angled triangle according to Pythagoras.",
Category = "Useful functions")]
public static double Hypotenuse(double a, double b)
{
return Math.Sqrt(a * a + b * b);
}
}

}

Govert van Drimmelen

unread,
Dec 8, 2011, 4:28:29 PM12/8/11
to Excel-DNA
Hi Karsten,

Before I try your sample, there's one thing you can try first for the
ribbon:

In your .dll you need to be sure that the ribbon class is ComVisible.
You can do this by marking the class as [ComVisible(true)], or by
setting COM-Visible for the assembly:
Go to the project properties, and select the Application tab. Then
click on Assembly Information and set the "Make assembly COM-Visible"
checkbox. (This sets the assembly-wide [assembly:ComVisible(true)]
attribute in your AssemblyInfo.cs file.)

By default (if there is no assembly-wide attribute) assemblies are
ComVisible, which is why the code in the .dna file works. But the
Visual Studio library template sets the assembly attribute to false by
default. The assembly-wide attribute can always be overridden by an
attribute on the particular class.

(Note that COM-Visible is a different setting to "Register for COM
Interop" which you should never set for an Excel-DNA library.)

If this is not the problem with your ribbon, I can look more closely.

-------

Getting to debug the Excel-DNA source can sometimes be a bit tricky -
I'm not sure why. It seems you have to get it debugging once by
attaching to Excel before it will work reliably.

What usually works for me is to set a breakpoint in the code - say in
XlAddIn.XlAutoOpen in the XlAddIn.cs file in the ExcelDna.Integration
project. Then start Excel externally. Then select "Debug->Attach to
Process" in Visual Studio, and select the Excel.exe process and the
right debuggers - both the Native Code option and the Managed option
for the right runtime version (by default 2.0 unless you have
RuntimeVersion="v4.0" in the .dna file.) Then attach the debugger, and
in Excel File->Open the ExcelDna.xll file. The breakpoint should be
hit and you can debug.

In future sessions I can then start Excel directly by setting it as
the Debug->Start External program and breakpoints in the Excel-DNA
code get hit fine.

---------

If you are debugging a user-defined function you won't see any Excel-
DNA code on the stack. Excel-DNA exports your function (with an on-the-
fly generated wrapper) directly as an unmanaged stub. The marshalling
code runs before (and after) your function, but not as part of the
call stack - it is interwoven by the runtime when the unmanaged stub
is created. So breakpoints in the marshalling code itself should work,
but you won't see that code on the call stack when a breakpoint in
your function is hit.

I'm not sure that's clear.... if you have a particular question I try
to be more specific.

--------

Good luck with your further Excel-DNA explorations.

Regards,
Govert

Karsten

unread,
Dec 9, 2011, 3:35:57 AM12/9/11
to Excel-DNA
Hi Govert,
thanks for your quick response.

Regarding the ribbon:
Sorry, I forgot to mention (and just rechecked) that I _did not_
forget to set
[assembly: ComVisible(true)]
in the AssemblyInfo.cs of my XL2Mem.dll-project. Though I'm still
sure, that I made some other small, silly fault ...
BTW: I'm running Excel 2010 on Windows 7 (32-bit).

Debugging:
I will try this.

Call stack when breaking on UDFs:
Ok, thanks for your explanation.


Regards,
Karsten

Karsten

unread,
Dec 9, 2011, 5:10:31 AM12/9/11
to Excel-DNA
Ok, now I can debug. I was not aware that I had to build a new XLL ...
sorry.

The problem is, that my XML2MemRibbon class is not recognized as being
derived from ExcelRibbon. Though the debugger shows the correct type,
the evaluation of

bool isRibbon = (t.BaseType == typeof(ExcelRibbon));
in
ExcelDna.Integration.AssemblyLoader.GetExcelAddIns

has isRibbon = false as the result.

When I patch "isRibbon" in the debugger to true, I later get an
exception in LoadCustomUI, since

ExcelRibbon excelRibbon = addIn.Instance as ExcelRibbon;

evaluates to null ... (again the runtime seems to see no relation
between XL2MemRibbon and ExcelRibbon).

All C# projects are set to framework 2.0.

Any idea?

Govert van Drimmelen

unread,
Dec 9, 2011, 7:36:50 AM12/9/11
to <exceldna@googlegroups.com>
Hi Karsten,

You probably have an extra copy of the ExcelDna.Integration.dll in your output directory. Mismatched versions cause trouble, and a copy is embedded in the .xll file.

In the properties for the reference to this library, set it to Copy Local=false, and delete any copy you have in the output directory now.

-Govert

> --
> You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
> To post to this group, send email to exce...@googlegroups.com.
> To unsubscribe from this group, send email to exceldna+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/exceldna?hl=en.
>
>
>
>

Karsten

unread,
Dec 9, 2011, 8:15:29 AM12/9/11
to Excel-DNA
Thx! Problem solved!

Karsten

On 9 Dez., 13:36, Govert van Drimmelen <gov...@icon.co.za> wrote:
> Hi Karsten,
>

> You probably have an extra copy of the ExcelDna.Integration.dll in your output directory. Mismatched versions  cause trouble, and a copy is embedded in the .xll file.
>
> In the properties for the reference to this library, set it to Copy Local=false,  and delete any copy you have in the output directory now.
>
> -Govert
>

> > For more options, visit this group at...
>
> Erfahren Sie mehr »

Reply all
Reply to author
Forward
0 new messages