XlCall.Excel Failing Inside ExcelAsyncUtil.QueueAsMacro

67 views
Skip to first unread message

Terry Aney

unread,
May 27, 2025, 10:51:43 AMMay 27
to Excel-DNA
I'm getting the following exception and I can't figure out how or why...

Message: Exception of type 'ExcelDna.Integration.XlCallException' was thrown.
HResult: -2146233088

My call stack looks something like this...

1.
ExcelAsyncUtil.QueueAsMacro( () =>
{
    try
    {
        application.Workbooks.Open( WorkbookState.FullName );
        application.GetReference( currentAddress ).Select();
    }

2.
public static ExcelReference GetReference( this MSExcel.Application application, string address ) => application.Range[ address ].GetReference();


3.
public static ExcelReference GetReference( this MSExcel.Range range )
{
    var sheet = (ExcelReference)XlCall.Excel( XlCall.xlSheetId, range.Worksheet.Name );

At the time of this call, Environment.CurrentManagedThreadId is 1.

Govert van Drimmelen

unread,
May 27, 2025, 11:59:33 AMMay 27
to Excel-DNA
Hi Terry,

If you don't open the workbook in the same macro call, does it then work?
Is the ExcelReference.Select() an extension method you have?
Can you make some small sample code that I can run?

-Govert

Terry Aney

unread,
May 27, 2025, 1:16:10 PMMay 27
to Excel-DNA
Some additional information

1. This error happens when I'm debugging ONLY when Analysis Toolpak and Analysis Toolpak - VBA are installed/activated as well.

2. Queuing another macro to do the select DOES work.  What were you thinking there?

3. If I run Excel and use the packed/deployed xll file, even with Analysis Tookpak addins activated it hardly ever errors.  There is a pattern that triggers it once in a while but I'm not sure.  I did the following:
- Open Excel with Analysis addins installed
- Test my code, no problems
- Close my file, add blank file, disable Analysis
- Close blank file
- Open my file, enable Analysis
- Run my code - error happens

But if I have analysis addins installed and close and open my file as much as I want to and run code (or simply enable/disable analysis addins without closing my file), no errors.

4. Select is my extension, but error happens before that so don't think it is involved, but this is my code.
    public static ExcelReference Select( this ExcelReference reference )
    {
        var workbookName = reference.WorkbookName();
        var sheetName = reference.SheetName();

        XlCall.Excel( XlCall.xlcActivate, workbookName );
        XlCall.Excel( XlCall.xlcWorkbookActivate, sheetName );
        XlCall.Excel( XlCall.xlcSelect, reference );
        return reference;
    }


5. Given info about Analysis, you still want small sample?  Do you have a small 'empty' template that is modifiable or should I just create a new addin on my own?

Thanks.

Govert van Drimmelen

unread,
May 27, 2025, 2:54:49 PMMay 27
to Excel-DNA
Hi Terry,

It sounds like you have things in hand now.
I suspect that sometimes Excel needs a bit of time for things to shake out after opening a workbook.
When you run on the main thread and mix the COM calls and C API (XlCall) calls I image Excel feeling a bit rushed with things.
I have run into Analysis Toolpak sometimes having weird interactions.

-Govert
Reply all
Reply to author
Forward
0 new messages