admin管理员组

文章数量:1122832

I'm trying to create a unit test for a function that runs in a background worker from Excel. My unit test can start the background worker fine but when the BW completes we use ExcelAsyncUtil.QueueAsMacro from ExcelDNA to process the result. This works in the normal user flow fine

private void BW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{ // This is the worker thread-completed code
    ExcelAsyncUtil.QueueAsMacro(() =>
    {
        string[] outputs = (string[])e.Result;
        if (outputs.Length < 1)
        {
            MessageBox.Show("Unable to retrieve result", title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            Common.ExitToExcel();
            return;
        }

However if I run this from my unit test without the Excel window visible I get the following error:

ExcelDna.Integration.dll!ExcelDna.Integration.ExcelAsyncUtil.QueueAsMacro(ExcelDna.Integration.ExcelAction action) Unknown

Exception thrown: 'System.NullReferenceException' in ExcelDna.Integration.dll An unhandled exception of type 'System.NullReferenceException' occurred in ExcelDna.Integration.dll Object reference not set to an instance of an object.

Excel is being opened via the Excel Interop when this error occurs in test mode

xl.Application excelApp = new xl.Application(); //open an Excel application with the given workbook
xl.Workbook workbook = excelApp.Workbooks.Open(filepath);

Is there something I need to do here to initialise the ExcelDNA Integration correctly when Excel is started via the Interop and running without a visible GUI?

本文标签: cUsing ExcelAsyncUtilQueueAsMacro when Excel is only running for Comm automationStack Overflow