1

我们有一个应用程序,它是使用 VSTO 构建的 Excel 插件。我们使用 Excel 作为报告工具。当我打开一个报表(工作簿)并选择一个图表,然后打开另一个工作簿时,我收到 COMException 错误。

System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A03EC    at Microsoft.Office.Interop.Excel._Worksheet.Copy(Object Before, Object After)

这仅在我选择了图表时发生。这是导致问题的代码。(DocumentView 是一个 Excel 工作簿。)

 private void CopyInitialSheets()
    {
        try
        {
            int sheetCount;
            Excel.Worksheet initialSheet = (Excel.Worksheet)StingerGlobal.AppData.ExcelApp.ActiveSheet;
            initialSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;  
            List<string> sheetNames = new List<string>(this.DocumentView.Sheets.Count);

            foreach (Excel.Worksheet sheet in this.DocumentView.Sheets)
            {

                sheetNames.Add(sheet.Name);
            }

            foreach (Excel.Worksheet sheet in _sheetReports.Keys)
            {
                Excel.Worksheet veryHiddenSheet = null;
                sheetCount = this.DocumentView.Sheets.Count;
                sheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;  
                sheet.Copy(Type.Missing, this.DocumentView.Sheets[sheetCount]);
                //Copy all the images, Charts, etc.  These don't copy over with the sheet.
                foreach (Excel.Shape o in sheet.Shapes)
                {
                    switch (o.Type)
                    {
                        case Microsoft.Office.Core.MsoShapeType.msoPicture:
                            o.CopyPicture();
                            break;
                        case Microsoft.Office.Core.MsoShapeType.msoChart:
                        case Microsoft.Office.Core.MsoShapeType.msoDiagram:
                        case Microsoft.Office.Core.MsoShapeType.msoCanvas:
                            o.Copy();
                            break;
                        default:
                            break;
                    }
                }

                //Find the sheet I just added because the after param to the Copy function is not respected.
                foreach (Excel.Worksheet newSheet in this.DocumentView.Sheets)
                {
                    if (sheetNames.Contains(newSheet.Name))
                        continue;  //Don't want this as it is an original sheet.
                    if (newSheet.Name.Contains("--VH--"))
                        continue;  //Don't want this as it is one of my copies.
                    //If code gets here, then I found the one I want
                    veryHiddenSheet = newSheet;
                }

                veryHiddenSheet.Name = MakeHiddenSheetName(sheet.Name, "--VH--");  //Do not localize.
                veryHiddenSheet.Visible = Excel.XlSheetVisibility.xlSheetVeryHidden;
            }

            //Make sure the first initial sheet is still active after we are done.
            ((Excel._Worksheet)initialSheet).Activate();
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }

这只在第二个工作簿上有问题,在第一个工作簿上工作正常。我最好的猜测是 Excel 以某种方式混淆了工作簿并导致了一些内存错误。我尝试了以下方法: - 清除剪贴板 - 取消选择活动工作表的图表(这是在第一个工作簿中) - 忽略异常(这停止了复制过程,并且在需要时找不到工作表) - 激活新工作簿的打开表

这些都没有奏效。到目前为止,唯一的解决方案是不要选择图表。:)

提前致谢。

4

1 回答 1

0

如果我有这个问题,我会在程序开始时选择一个单元格。

在 VBA 中:application.GoTo Activeworkbook.Worksheets(1).Range("A1")

于 2013-10-08T22:11:42.223 回答