Interop Word&Excel:无法关闭Word文档中嵌入的Excel工作簿

问题描述 投票:0回答:1

当我关闭Word文档中嵌入的Excel工作簿时出现错误。该文档只有一个Excel图表。没什么:文字等...

这是代码:

Application _application = new Application
{
    Visible = false,
};
object oFilename = fileName;
object oFormat = WdOpenFormat.wdOpenFormatXMLDocument;
_document = _application.Documents.Open(
     ref oFilename, ref _missing, ref _faux, ref _missing, ref _missing, ref _missing,
     ref _missing, ref _missing, ref _missing, ref oFormat, ref _missing, ref _faux,
     ref _missing, ref _missing, ref _missing, ref _missing);

_document.Activate();

InlineShape shape1 = _document.InlineShapes[1];
Microsoft.Office.Interop.Word.Chart chart1 = shape1.Chart;
Workbook wb1 = chart1.ChartData.Workbook;
wb1.Application.WindowState = XlWindowState.xlMinimized;
Worksheet ws1 = wb1.Worksheets["Graphe"];
ws1.Cells[2, "A"].Value = 1000;

// --- THIS IS THE LINE WHICH HANDLE EXCEPTION
wb1.Close(Type.Missing, Type.Missing, Type.Missing);
// ---

_document.Close(ref _missing, ref _missing, ref _missing);
_application.Application.Quit(ref _missing, ref _missing, ref _missing);

让我们看看异常(似乎没有用..):HRESULT异常:0x800A03EC

堆栈跟踪:

à Microsoft.Office.Interop.Excel._Workbook.Close(Object SaveChanges, Object Filename, Object RouteWorkbook)
à TestFile.Program.Main(String[] args) dans C:\[Users..]\MyProject\Program.cs :ligne 53

感谢您的帮助,问候

c# excel ms-word office-interop
1个回答
0
投票

我找到了解决方法...

替换:

wb1.Close(Type.Missing, Type.Missing, Type.Missing);

使用:

wb1 = null;

/!\但请谨慎处理wb1,因为我们仍在讨论COM对象

© www.soinside.com 2019 - 2024. All rights reserved.