xlfGetDocument and IsMacroType = true error

243 views
Skip to first unread message

Forna

unread,
Mar 17, 2012, 6:12:13 PM3/17/12
to Excel-DNA
Hi there,

I am trying to use the XlCall.xlfGetDocument function to return the
last used row (ArgNum 10) and last used column (ArgNum 12) in the
active worksheet.
I have read in the forums that xlfGetDocument requires the IsMacroType
= true.
So I have added it to the ExcelFunction but now I get the following
error in Excel.

Run-time error '1004':
Cannot run the macro 'TestExcelDNA'. The macro may not be available in
this workbook or all macros may be disabled.

I am starting the call to the C# function in VBA:
Call Application.Run("TestExcelDNA")

The C# code is the following:
public static class TestClass
{
[ExcelFunction(Name = "TestExcelDNA", IsMacroType = true)]
public static void TestExcelDNA()
{
ExcelReference ActiveSheetID =
(ExcelReference)XlCall.Excel(XlCall.xlSheetId);
string ActiveSheetName =
(string)XlCall.Excel(XlCall.xlSheetNm, ActiveSheetID);
string ActiveSheetRow =
(string)XlCall.Excel(XlCall.xlfGetDocument, 10, ActiveSheetName);
XlCall.Excel(XlCall.xlcMessage, true, ActiveSheetRow);
}
}

Thanks

Govert van Drimmelen

unread,
Mar 17, 2012, 8:25:55 PM3/17/12
to Excel-DNA
Hi Paolo,

The [ExcelFunction(...)] attribute, and that IsMacroType=true flag,
are only used for worksheet UDF functions.
By default, worksheet functions would not be able to read the
xlfGetDocument information. But macros can be registered as "macro
sheet functions", which enables such access from the UDF.

In the case of your example, you are registering a macro (returns
'void') and so you'd use the [ExcelCommand] attribute instead.
All macros have access to the information functions, so no need for an
"IsMacroType=true" in this case.

In fact (unless you have set ExplicitExports='true' in your .dna file)
your code should work fine if you just remove the [ExcelCommand]
attribute. (With the ExplicitExports='true' you'd have to use an
[ExcelCommand] attribute to mark the method for registration.)

Regards,
Govert

Forna

unread,
Mar 18, 2012, 6:36:48 PM3/18/12
to Excel-DNA
Thanks, it works now.
Reply all
Reply to author
Forward
0 new messages