如何检查Word 2007是否调用自动保存到word插件中?
我在 DocumentBeforeSave 事件处理程序中有一些功能。
仅当用户手动调用“保存”(按“保存”按钮)时,这才应该起作用。
但是word 2007有自动保存功能,并且每次自动保存工作时都会抛出DocumentBeforeSave事件。如何检查是否通过自动保存调用保存或用户手动调用保存?
I have some functionality inside DocumentBeforeSave event handler.
That's should work only when user manually invoke Save (press Save button).
But word 2007 have autosave function and event DocumentBeforeSave throws each time when autosave work. How to check that save is invoked over Autosave or User manually invoke Save?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来没有内置的方法可以做到这一点,因为对象模型根本不支持它(根据 此链接),但您可以使用 VBA 覆盖默认的保存热键和单击按钮,然后将这些调用发送到您的 .NET 程序集(根据 此链接)。只需确保随后手动调用保存以确保文档实际保存即可。
It looks like there's no build-in way of doing that because the object model simply doesn't support it (per this link), but you can use VBA to override the default save hotkey and button click, and send those calls to your .NET assembly (per this link). Just make sure you invoke save manually afterwards to make sure the document actually saves.
实际上有多种方法可以区分。
选项1(最好)
选项2(我在找到选项1之前所做的)
从a中拦截FileSave(和FileSaveAs)命令丝带:
在 SaveIntercept 方法中设置一个标志,并将 CancelDefault 设置为 false,以便保存继续。
还要实现 BeforeDocument_BeforeSave,并检查那里的标志。如果设置了标志,则为手动保存,否则为自动保存(或可能来自另一个加载项;不确定是否有效)。
这包括通过后台按钮、快速访问工具栏按钮和保存快捷方式进行保存(即使它们重新定义了键盘快捷键)。
有趣的是,还有一种方法可以在保存后进行判断,如此处 (此处更新版本)。
There are actually multiple ways to tell the difference.
Option 1 (best)
Option 2 (what I did before finding option 1)
Intercept the FileSave (and FileSaveAs) command from a ribbon:
Set a flag in the SaveIntercept method, and set CancelDefault to false so the save continues.
Also implement BeforeDocument_BeforeSave, and check the flag there. If the flag was set, it was manual, otherwise it's an autosave (or potentially comes from another Add-In; not sure if that works).
This covers saves through the backstage buttons, the quick access toolbar buttons, and the save shortcut (even if they redefine the keyboard shortcut)
Interestingly, there is a way to tell after the save as well, as described here (updated version here).