Excel的挂在点的消息框中问我是否要保存工作簿 [英] Excel hangs at the point where the message box asks if I want to save the workbook

查看:145
本文介绍了Excel的挂在点的消息框中问我是否要保存工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类报告巫婆包含一个方法 RunExcelReporting 。它知道什么工作簿打开,并通过控制表来运行什么程序。



正如我刚才拼对照表不正确的宏之一。结果
控制台应用程序测试创建<$ C的一个实例$ C>报告及运行 RunExcelReporting 方法,但随后停止在一个点,Excel中diplaying,询问用户是否希望将消息框保存工作簿。这对我来说是一个糟糕的中途之家一点 - 我宁愿有某种错误信息或者我想工作簿关闭不保存更改。我怎样才能修改下面的代码,以帮助?



 私人无效RunExcelReporting(INT X){

Excel.Application excelApp = NULL;
Excel.Workbook书= NULL;
尝试{

excelApp =新Excel.Application();
excelApp.Visible = TRUE;
=书excelApp.Workbooks.Open((串)MyReportRow [XLfile+ X +_path] +(字符串)MyReportRow [XLfile+ X +_Name]); //空,真正);

//通过每本书
四种可能的宏环路(; J< INT J = 1 = 4; J ++){
如果(ISNULL(MyReportRow [ XLfile+ X +_Macro+ J])== FALSE){
excelApp.Run((串)MyReportRow [XLfile+ X +_Macro+ J]);
}
}
book.Close(假,Type.Missing,Type.Missing);
}
赶上(例外前){
Console.WriteLine(ex.ToString());
}
终于{
如果(书!= NULL){
//book.Close(false,Type.Missing,Type.Missing);
书= NULL;
}
如果(excelApp!= NULL){
excelApp.Application.Quit();
excelApp.Quit();
excelApp = NULL;
}
}
}


解决方案

看来问题在于:Excel不冒泡的错误到控制台上;错误是在Excel即一个单独的进程到控制台发生。



我需要程度上应用相当多的考虑到这一点。


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

解决方案

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.

这篇关于Excel的挂在点的消息框中问我是否要保存工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆