C# MS Interop Word、Excel多个打开的文件文档关闭前处理
我的应用程序中有一个类似windows资源管理器的模块。 我想在该模块中打开和关闭word和excel文件。 当我同时打开f.e.4-5文件时,关闭处理程序无法正常工作 问题是:有时在关闭Word应用程序后,我的应用程序不会在wordDocEvents\u DocumentBeforeClose函数开头的断点处停止。 似乎与进程间通信有关,因为每个打开的文档都是一个新的进程 如果它是已知的问题,请帮助,否则我会尝试在我的代码中做一些事情 代码快照:C# MS Interop Word、Excel多个打开的文件文档关闭前处理,c#,excel,ms-word,office-interop,C#,Excel,Ms Word,Office Interop,我的应用程序中有一个类似windows资源管理器的模块。 我想在该模块中打开和关闭word和excel文件。 当我同时打开f.e.4-5文件时,关闭处理程序无法正常工作 问题是:有时在关闭Word应用程序后,我的应用程序不会在wordDocEvents\u DocumentBeforeClose函数开头的断点处停止。 似乎与进程间通信有关,因为每个打开的文档都是一个新的进程 如果它是已知的问题,请帮助,否则我会尝试在我的代码中做一些事情 代码快照: if (_wordApp == null) {
if (_wordApp == null)
{
_wordApp = new Word.Application();
_wordDocEvents = (Word.ApplicationEvents4_Event)_wordApp;
if (!isLocked)
{
//_wordDocEvents.Quit += new Word.ApplicationEvents4_QuitEventHandler(wordDocEvents_Quit);
_wordDocEvents.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(wordDocEvents_DocumentBeforeClose);
_wordDocEvents.DocumentBeforeSave += new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(wordDocEvents_DocumentBeforeSave);
_wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
}
}
_wordApp.Visible = true;
Word.Document oDoc = _wordApp.Documents.Open(fileName, null, isLocked);
oDoc.Activate();
_wordApp.Visible = true;
_wordApp.Activate();
关闭处理程序:
void wordDocEvents_DocumentBeforeClose(Word.Document doc, ref bool Cancel)
{
if (!doc.Saved)
{
MessageBoxResult res = MessageBox.Show(ApplicationValues.GetResourceString("library_file_save_changes"), "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes, System.Windows.MessageBoxOptions.DefaultDesktopOnly);
switch (res)
{
case MessageBoxResult.Yes:
doc.Save();
break;
case MessageBoxResult.No:
doc.Saved = true;
break;
default:
Cancel = true;
return;
}
}
interopFileBeforeCloseHandler(doc.FullName, doc.ReadOnly);
}
这是一个较旧的线程,但我在msdn上找到此线程后发现了它: 因此,一个相当简单的解决方案是将所有创建/加载的文档存储到一个数组/列表/。。。因此,当垃圾收集器认为需要处理它们时,它不会处理它们,从而清除所有事件处理程序
对我来说,这是一种魅力;) 你能弄明白吗?我们遇到了同样的问题。这个问题仍然没有答案…?:(我也有同样的问题…有时关闭事件触发器,有时没有…我只是停止使用MS Interop这是我的解决方案抱歉:)