【问题标题】:Auto-Save fires `DocumentBeforeSave` event - How to circumvent this?自动保存触发“DocumentBeforeSave”事件 - 如何规避这个?
【发布时间】:2017-01-03 09:51:13
【问题描述】:
当文件被保存(故意)时,我想执行某个操作。
这一切都适用于以下代码:
Application.DocumentBeforeSave += new word.ApplicationEvents4_DocumentBeforeSaveEventHandler(ThisAddIn_BeforeSave);
只有一个问题,当对文档进行更改并且“自动恢复”功能保存此更改时,也会触发此事件。
有没有办法绕过此操作或至少检测它是否是自动保存?
【问题讨论】:
标签:
c#
vsto
office-addins
【解决方案1】:
所以我设法在this 网站上找到了这个问题的答案。
本质上,这利用了我假设的 Word 应用程序的 VBA 属性。
object oBasic = Application.WordBasic;
object fIsAutoSave =
oBasic.GetType().InvokeMember(
"IsAutosaveEvent",
BindingFlags.GetProperty,
null, oBasic, null);
if (int.Parse(fIsAutoSave.ToString()) == 1)
MessageBox.Show("Is AutoSave");
else
MessageBox.Show("Is regular save");
此解决方案似乎适用于 Office 2007 及更高版本。