Cod*_*e K 1 vba ms-word savefiledialog before-save
我正在使用以下 VBA 代码在保存 Word 文档时显示一个消息框,
Public WithEvents appWord as Word.Application
Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Do you really want to " _
& "save the document?", _
vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub
Run Code Online (Sandbox Code Playgroud)
这段代码是在一个类中编写的。但这不起作用。保存时没有任何反应。这里有什么问题?
我让它工作。感谢AnalystCave.com的帮助。这就是我所做的:
我创建了一个名为EventClassModule的新类并复制了以下代码,
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Do you really want to " _
& "save the document?", _
vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub
Run Code Online (Sandbox Code Playgroud)
然后创建了一个名为mdlEventConnect的模块并复制了以下代码,
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
Run Code Online (Sandbox Code Playgroud)
之后在ThisDocument上复制了以下代码,
Private Sub Document_New()
Register_Event_Handler
End Sub
Private Sub Document_Open()
Register_Event_Handler
End Sub
Run Code Online (Sandbox Code Playgroud)
保存文档并重新打开它。现在,当我尝试保存文档时,它完美地执行了DocumentBeforeSave事件。