whytheq
whytheq

Reputation: 35587

Excel hangs at the point where the message box asks if I want to save the workbook

I've got a Class report witch includes a method RunExcelReporting. It knows what workbook to open and what routines to run via a control table.

As a test I've just spelled one of the macros incorrectly in the control table.
The console application creates an instance of report and runs the RunExcelReporting method but then stops at a point where Excel is diplaying the message box which asks if the user would like to save the workbook. This is a bad halfway house point for me - I'd rather have some sort of error message OR I'd like the workbook to close without saving changes. How can I amend the following code to help?

private void RunExcelReporting(int x) {

    Excel.Application excelApp = null;
    Excel.Workbook book = null;       
    try {

        excelApp = new Excel.Application();
        excelApp.Visible = true;
        book = excelApp.Workbooks.Open((string)MyReportRow["XLfile" + x + "_Path"] + (string)MyReportRow["XLfile" + x + "_Name"]);//,null,true);

            //loop through the four possible macros in each book
        for (int j = 1; j <= 4; j++) {
            if (IsNull(MyReportRow["XLfile" + x + "_Macro" + j]) == false) {
                excelApp.Run((string)MyReportRow["XLfile" + x + "_Macro" + j]);
            }
        }
        book.Close(false, Type.Missing, Type.Missing);
    }
    catch (Exception ex) {
        Console.WriteLine(ex.ToString());
    }
    finally {
        if (book != null) {
            //book.Close(false, Type.Missing, Type.Missing);
            book = null;
        }
        if (excelApp != null) {
            excelApp.Application.Quit();
            excelApp.Quit();
            excelApp = null;
        }
    }
}

Upvotes: 2

Views: 851

Answers (2)

whytheq
whytheq

Reputation: 35587

Seems that the problem is that Excel isn't bubbling up the error to the console; the error is happening within Excel i.e a separate process to the console.

I'll need to extent the application quite a bit to account for this.

Upvotes: 0

Codo
Codo

Reputation: 78915

Try to add (somewhere before book.Close):

excelApp.DisplayAlerts = false

Upvotes: 1

Related Questions