2014-04-06 67 views
1

我有一个像我的应用程序中的Windows资源管理器模块。 我想处理该模块中的单词和excel文件的打开和关闭。 当我开放f.e. 4-5个文件在同一时间,关闭处理程序无法正常工作。MS互操作Word,Excel多个打开的文件DocumentBeforeClose处理

问题是:有时在关闭Word应用程序后,我的应用程序不会在wordDocEvents_DocumentBeforeClose函数的开始处停止断点。 似乎与相关的进程间通信有关,因为每个打开的文件都是一个新的进程。

如果它是已知的问题,请帮助,否则我会尝试在我的代码中做一些事情。

代码快照:

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); 

} 
+0

你能弄明白吗?我们遇到同样的问题。 – animaonline

+0

这个问题仍然没有答案...?:(我也有同样的问题..有时关闭事件触发器,有时它不... –

+0

我只是停止使用MS互操作,这是我的解决方案对不起:) – artos

回答

0

这是一个较旧的,但我发现这个线程在MSDN后想通了: workbookbeforeclose-event-does-not-fire

所以一相当容易的解决方案是将所有创建/加载的文档存储到一个数组/列表//...所以垃圾收集器不会处理它们,当它认为它需要做所以,从而清除所有事件处理程序。

工作就像一个魅力对我来说;)