Shutdown event is not always fired when Saving a Word document during BeforeClose event

只谈情不闲聊 提交于 2019-12-13 14:26:45

问题


I have a Microsoft Word Add-in using VSTO and c#.

For each document, I am using the document ShutDown event to do cleanup of my parent object and the BeforeClose event for pre-close validation of my parent object. My validation needs the document to be saved and tries to do that in the BeforeClose event.

If the document has never been saved before, the Save As dialog appears. If the user clicks Cancel a COMException is thrown and I catch that properly. Then the Save/Don't Save/Cancel dialog appears (which normally shows up for a document that is about to be closed with unsaved changes). If the user saves from the first Save As dialog or saves from the second save dialog the ShutDown event is fired correctly. However, If the user clicks Don't Save on the second dialog, the ShutDown event is not fired.

private void WordDocument_BeforeClose(object sender, System.ComponentModel.CancelEventArgs e)
{
  try
  {
    this.WordDocument.Save();
  }
  catch (System.Runtime.InteropServices.COMException a)
  {
    log.Error(a.Message);
  }
}

private void WordDocument_Shutdown(Object sender, EventArgs e)
{
  // Parent cleanup.
}

来源:https://stackoverflow.com/questions/12749411/shutdown-event-is-not-always-fired-when-saving-a-word-document-during-beforeclos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!