Word VBA - DocumentBeforeSave 事件? [英] Word VBA - DocumentBeforeSave event?
问题描述
我使用以下 VBA 代码在保存 Word 文档时显示消息框,
I am using the following VBA code to make a message box appear while saving the Word document,
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
这段代码是在一个类中编写的.但这不起作用.保存时没有任何反应.这里有什么问题?
This code was written in a Class. But this does not work. Nothing happens when saving. What is the issue here?
推荐答案
我成功了.感谢 AnalystCave.com 的帮助.这就是我所做的:
I made it to work. Thanks to AnalystCave.com for the help. This is what I did:
我创建了一个名为 EventClassModule 的新类并复制了以下代码,
I create a new class named EventClassModule and copied the following code,
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
然后创建一个名为mdlEventConnect的模块并复制以下代码,
Then created a module named mdlEventConnect and copied the following code,
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
此后在ThisDocument上复制了以下代码,
After this on the ThisDocument copied the following code,
Private Sub Document_New()
Register_Event_Handler
End Sub
Private Sub Document_Open()
Register_Event_Handler
End Sub
保存文档并重新打开它.现在,当我尝试保存文档时,它完美地执行了 DocumentBeforeSave 事件.
Saved the document and re-opened it. Now when I try to save the document it executed the DocumentBeforeSave event perfectly.
这篇关于Word VBA - DocumentBeforeSave 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!