如何确保DocumentBeforeSave代码仅在当前文档上运行

时间:2017-01-31 21:55:14

标签: ms-word word-vba

我有一个宏,当我按照此处所述保存我的文档时运行:https://msdn.microsoft.com/en-us/library/office/ff838299.aspx但是,当保存任何打开的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

1 个答案:

答案 0 :(得分:1)

Word的DocumentBeforeSave事件提供Doc参数,该参数包含触发事件的文档。测试这是否是您感兴趣的Document对象。从您的问题来看,宏似乎是文档的一部分,所以类似于:

If Doc Is ThisDocument Then Doc.Save