互操作字词带有.NET代码的Excel [英] Interop Word & Excel with .NET Code

查看:86
本文介绍了互操作字词带有.NET代码的Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在.NET代码中使用Interop Word和Excel来覆盖Word/Excel的一些功能.这是我遇到一些问题的时候,而您是获得正确答案的唯一希望:confused:

我需要覆盖office的保存"功能,并且需要保存我想要的Doc,然后需要关闭当前文档.

为此,我正在使用以下代码:
字词:

Hello Everyone,

I am using Interop Word and Excel in my .NET code to override few of the functionalities of Word/ Excel. This is when I am getting few problems and you are the only hope left for the correct answer :confused:

I need to override the Save function of office and needs to save the Doc I want and then needs to close the current document.

For this, I am using the following codes:
Word:

MVSTODocument.SaveAs(ref filePath, ref fileSaveFormat, ref fileLockComments, ref missing, ref isAddToRecentFiles , ref missing, ref isFileReadOnly, ref fileEmbedTrueTypeFonts, ref fileSaveNativePictureFormat, ref fileSaveFormsData , ref isSaveAsAOCELetter, ref fileMSOEncoding, ref fileInsertLineBreaks, ref fileAllowSubstitutions, ref fileLineEncoding, ref fileAddBiDiMarks);

MVSTODocument.Close(ref fileSaveOptions, ref fileSaveFormat, ref LbIsToSave)



Excel:



Excel:

MVSTOWorkbook.SaveCopyAs(LstrTempFileFullPath);
InteropExcel_.Workbook workBook = MVSTOWorkbook.Application.Workbooks.Open(LstrTempFileFullPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing);

workBook.Close(false, LstrTempFileFullPath, null);
Marshal.ReleaseComObject(workBook);
MVSTOWorkbook.Close(false, objApp.ActiveWorkbook.Name, Type.Missing);



我之所以不能在excel中使用另存为"的原因是因为当我从Office 2007迁移到Office 2010时,它停止工作了.

现在我面临的问题是-运行此代码时.它与Excel完美配合,但是对于Word,它会引发COM异常,从而使Word App崩溃.



The Reason I din''t use ''SaveAs'' in excel because it stopped working when I moved from Office 2007 to Office 2010.

Now the Problem I am facing is - When I run this code. It works perfectly fine with Excel but for Word it throws a COM exception which crashes the Word App.

31982216 mov esi,dword ptr [edi+2ACh]

Unhandled exception at 0x31982216 in WINWORD.EXE: 0xC0000005: Access violation reading location 0x000002ac.



奇怪的是我的调试器在我的.NET代码完成之前就没问题了.
我唯一注意到的可能是因为只要有跨应用程序调用,COM就会调用Workbook.Save()/Document.Save()方法. Excel实现了空检查,而Word没有.真的吗?我可以停止这种奇怪的行为吗?

任何指针将不胜感激.
谢谢.

有任何建议吗?



The weird thing is my the debugger leaves perfectly fine till my .NET code.
The only thing I noticed is that may be because COM calls the Workbook.Save()/ Document.Save() method whenever my there is a cross app call. Excel has a null check implemented and Word does not have. Is that true? Can I Stop this weird behavior?

Any pointers will be appreciated.
Thanks.

Any suggestions?

推荐答案

我已解决此问题,方法是使用计时器刻度线异步调用我的关闭"文档调用.而且效果很好!!!

虽然,我真的不知道为什么吗? :)

但是现在我面临另一个问题,即即使我在用户单击``另存为''时杀死了我的保存文档事件,它仍然向我显示了愚蠢的``另存为''对话框窗口.

为什么Word总是表现得如此古怪?相同的东西在Excel中可以完美地工作...
I have solved this issue by calling my Close document call asynchronously using a timer tick. And it works fine !!!

Though, I really don''t know that why? :)

But now I am facing another issue i.e. even though I am killing my Save document event whenever user clicks ''Save As'', it still shows me that stupid ''Save As'' dialog window.

Why Word behaves so Weird always ?! The same thing works perfect in Excel...


这篇关于互操作字词带有.NET代码的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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