打开文件对话框后添加Worbook的异常 [英] Exception adding Worbook after open file dialog

查看:68
本文介绍了打开文件对话框后添加Worbook的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个让我发疯的问题。希望你能帮帮我。

这是代码:

私有空按钮1_Click(对象发送者,EventArgs e)
{
试试 {
string pathToTemplate =" C:\\QuoteTemplate.xltx" ;;
Excel.Workbooks wbs = Globals.ThisAddIn.Application.Workbooks;
Excel.Workbook template = wbs。添加(pathToTemplate);
template.Close(Type.Missing,Type.Missing,Type.Missing);
}
catch(Exception ex)
{※MessageBox .Show(ex.Message);
}
}


private void button2_Click_1(object sender,EventArgs e)
{OpenFileDialog对话框=新的O. penFileDialog();
dialog.RestoreDirectory = true;
if if(dialog.ShowDialog()== DialogResult.OK)
{
//做某事......
按钮2打开文件对话框。

两个代码都正常。我的问题如下:
当我首先执行button2(打开文件对话框)然后按钮1(加载模板),然后按钮1中的代码行
Excel.Workbook template = wbs.Add(pathToTemplate)提出异常。

有没有人知道原因?

非常感谢你。


照了elastonova
>

解决方案

嗨Elastonova

这是Excel 2007,我接受了吗?功能区,操作窗格中的这些按钮或其他位置(请注明)?

您能否提供有关退回异常的更多信息?消息中包含哪些信息?

如果您发生任何事情,"//做某事......"?如果是,那究竟是什么?

对于你的代码,我没有看到任何问题,但是也许异常消息会给我们提供更多信息......我不知道是否对话来自button2_Click事件的对象可能是"锁定"的。一些东西。但是因为它显示为非模态(ShowDialog),所以当button1_Click运行时它仍然无法打开。如果您明确地释放相应程序结束时涉及的对象,它是否有任何区别:
dialog = null:
template = null;
wbs = null;


Hi,

I have a problem that makes me crazy. Hopefully you can help me.

This is the code:

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string pathToTemplate = "C:\\QuoteTemplate.xltx";
                Excel.Workbooks wbs = Globals.ThisAddIn.Application.Workbooks;
                Excel.Workbook template = wbs.Add(pathToTemplate);
                template.Close(Type.Missing, Type.Missing, Type.Missing);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {           
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //Do something...
            }
        }

The button1 tries to load an Excel template file (*.xltx) into the workbook collection.

The button2 opens a file dialog.

Both code works fine. My problem is the following:
when I perform button2 at first (open file dialog) and then button1 (load template), then the code row
Excel.Workbook template = wbs.Add(pathToTemplate) in button1 raises an exception.

Does anybody know why?

Thank you very much.


elastonova

解决方案

Hi Elastonova

This is Excel 2007, I take it? Are these buttons on the Ribbon, in an Actions Pane, or somewhere else (please specify)?

Can you give us more information about the Exception being returned, please? What information is in the message?

Also, is anything happening where you have "//Do something..."? If yes, what, exactly?

Right off-hand, I don't see anything wrong with your code, but perhaps the Exception message will give us more information... I do wonder whether the dialog object from the button2_Click event is perhaps "locking" something. But since it's being shown non-modal (ShowDialog) it can't still be open when button1_Click runs. Does it make any difference if you expressly release the objects involved at the end of the respective procedures:
     dialog = null:
     template = null;
     wbs = null;


这篇关于打开文件对话框后添加Worbook的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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