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

牧云@^-^@ 提交于 2019-12-11 02:41:26

问题


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;
        }
    }
}

回答1:


Try to add (somewhere before book.Close):

excelApp.DisplayAlerts = false



回答2:


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.



来源:https://stackoverflow.com/questions/13879691/excel-hangs-at-the-point-where-the-message-box-asks-if-i-want-to-save-the-workbo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!