该事件在创建新文档、打开已有文档或激活其他文档时发生。
Private Sub object_DocumentChange()
object 在类模块的事件中声明的 Application 类型对象。有关使用 Application 对象事件的详细内容,请参阅使用 Application 对象事件。
本示例在文档焦点转移时,提示用户是否保存所有其他已打开的文档。为了看到本例的执行结果,本例提供的代码必须保存在一个类模块中,而且该类中的实例必须正确初始化;有关完成上述任务的指导,请参阅使用 Application 对象事件。
Public WithEvents appWord as Word.Application
Private Sub appWord_DocumentChange()
Dim intResponse As Integer
Dim strName As String
Dim docLoop As Document
intResponse = MsgBox("Save all other documents?", vbYesNo)
If intResponse = vbYes Then
strName = ActiveDocument.Name
For Each docLoop In Documents
With docLoop
If .Name <> strName Then
.Save
End If
End With
Next docLoop
End If
End Sub