Word VBA-DocumentBeforeSave事件? [英] Word VBA - DocumentBeforeSave event?

查看:548
本文介绍了Word VBA-DocumentBeforeSave事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下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

此代码是在Class中编写的.但这是行不通的.保存时没有任何反应.这是什么问题?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆