自动保存触发 `DocumentBeforeSave` 事件 - 如何避免这种情况?
Auto-Save fires `DocumentBeforeSave` event - How to circumvent this?
保存文件时(有意)我想执行某个操作。
这一切都适用于以下代码:
Application.DocumentBeforeSave += new word.ApplicationEvents4_DocumentBeforeSaveEventHandler(ThisAddIn_BeforeSave);
只有一个问题,当对文档进行更改并且 'Autorecover' 函数保存此更改时也会触发此事件。
有没有办法绕过此操作或至少检测它是否是自动保存?
所以我设法在 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 及更高版本。
保存文件时(有意)我想执行某个操作。
这一切都适用于以下代码:
Application.DocumentBeforeSave += new word.ApplicationEvents4_DocumentBeforeSaveEventHandler(ThisAddIn_BeforeSave);
只有一个问题,当对文档进行更改并且 'Autorecover' 函数保存此更改时也会触发此事件。
有没有办法绕过此操作或至少检测它是否是自动保存?
所以我设法在 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 及更高版本。