访问活动图表时Excel 2016崩溃 [英] Excel 2016 crashes when accessing active chart

查看:114
本文介绍了访问活动图表时Excel 2016崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VSTO项目简单代码:

VSTO project with simple code:

     private Excel.Workbook lastWorkbook;     

private void ThisAddIn_Startup(object sender, System.EventArgs e){ var app = Globals.ThisAddIn.Application; app.WorkbookActivate += AppOnWorkbookActivate; } private void AppOnWorkbookActivate(Excel.Workbook wb){ try{ if (lastWorkbook != null){ var chart = lastWorkbook.ActiveChart; } } catch (Exception e){ Console.WriteLine(e); } lastWorkbook = wb; }

现在执行以下操作:

1。在Excel 2016 64bit中运行该项目。您应该打开一个工作簿。

1. Run the project in Excel 2016 64bit. You should have one workbook opened.

2。转到文件 - >新建并打开第二个工作簿。

2. Go to File-> New and open a second workbook.

3。切换到第一个工作簿并关闭它。

3. Switch to the first workbook and close it.

4。这会导致未捕获的异常并导致Excel崩溃。

4. This causes an exception which is not caught and crashes Excel.

如果在Excel 2010或Excel 2013中执行此操作,则不会发生这种情况。任何解决方法?如何确定当前工作簿(在工作簿激活事件中)之前处于活动状态的工作簿是否已关闭,以便在这种情况下我可以避免访问ActiveChart?

If you do this in Excel 2010 or Excel 2013 this does not happen. Any workaround? How can I determine if the workbook that was active before the current one (in workbook activate event) was closed so that I can avoid accessing ActiveChart in this case?

推荐答案

Hi Mitja,

Hi Mitja,

我认为你的问题是当你关闭第一个工作簿时,lastWorkbook现在没有发布,然后第二个工作簿活动,这个异常发生了。当CLR检测到某些内容出现严重错误时,会抛出此异常。

I think your issue was caused by that when you close first workbook, the lastWorkbook is not released right now, then the second workbook actives, this exception happened. This exception is thrown when the CLR detects that something has gone horribly wrong.

如果第一个工作簿关闭,如果不需要获取ActiveChart,则可以尝试关闭第一个工作簿时将lastWorkbook设置为null。这是一个简单的代码:

If you do not need get ActiveChart if the firstworkbook close, you could try to set the lastWorkbook null when you close first workbook. Here is a simple code:

private Excel.Workbook lastWorkbook;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            var app = Globals.ThisAddIn.Application;
            app.WorkbookActivate += AppOnWorkbookActivate;
            app.WorkbookBeforeClose += App_WorkbookBeforeClose;
        }
        private void App_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
        {
            if (lastWorkbook == Wb)
            {
                lastWorkbook = null;
            }
        }
        private void AppOnWorkbookActivate(Excel.Workbook wb)
        {
            try
            {
                if (lastWorkbook != null)
                {
                    var chart = lastWorkbook.ActiveChart;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            lastWorkbook = wb;
        }

最好的问候,

Best Regards,

Edward


这篇关于访问活动图表时Excel 2016崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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