互操作字词Excel:无法关闭Word文档中嵌入的Excel工作簿 [英] Interop Word & Excel : Can't close an Excel workbook embedded in Word document

查看:118
本文介绍了互操作字词Excel:无法关闭Word文档中嵌入的Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got an error when i'm closing the Excel workbook embedded in a Word document. This document got only one Excel chart. Nothing else : texts, etc...

这是代码:

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

Let's see the exception (seem to be useless..) : Exception de 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

感谢您的帮助, 问候

推荐答案

我找到了解决方法...

I found a workaround...

替换:

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

使用:

wb1 = null;

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

/!\ BUT be careful about the Dispose of wb1 because we are still talking about a COM object

这篇关于互操作字词Excel:无法关闭Word文档中嵌入的Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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