我想检测用户何时按ctrl-S或使用VBA excel宏代码单击Microsoft Word的保存选项。
我找到了关于Save changes while closing和Detecting if a document is getting close的相关链接,但我无法找到一些用于检测word文档保存的示例代码。
任何帮助都会非常感激。
由于
答案 0 :(得分:1)
不幸的是(或幸运的是)Word不像excel那样工作,并且键绑定遵循不同的逻辑。因此,在Word中,你应该尝试这样的东西,绑定 Ctrl + S 。我们的想法是,在打开文件时,您告诉应用程序将 Ctrl + S 绑定到SaveMe
Sub。
Option Explicit
Private Sub Document_Open()
With Application
.CustomizationContext = ThisDocument
.KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyS), KeyCategory:=wdKeyCategoryCommand, Command:="SaveMe"
End With
End Sub
Public Sub SaveMe()
MsgBox "User Saved"
End Sub
将代码放入ThisDocument
:
到目前为止,它只适用于Ctrl + S。如果您通过菜单进行保存,则不会遵循此逻辑。这就是你应该做的:
对于常规解决方案,请按照以下说明操作:
我。创建clsWord
并将以下代码放入其中:
Option Explicit
Public WithEvents appWord As Word.Application
Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Call SaveMe
End Sub
II。将ThisDocument
中的代码更改为:
Option Explicit
Dim myWord As New clsWord
Private Sub Document_Open()
With Application
.CustomizationContext = ThisDocument
.KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyS), _
KeyCategory:=wdKeyCategoryCommand, Command:="SaveMe"
End With
Set myWord.appWord = Word.Application
End Sub
III。在模块中:
Option Explicit
Public Sub SaveMe()
MsgBox "User Saved"
End Sub
部分想法来自MSDN - https://msdn.microsoft.com/en-us/library/office/ff838299.aspx但是那里的代码并不完整:)