會在建立新文件、開啟現有文件,或是當其他文件變成使用中文件時發生。
語法
expression.**DocumentChange' ()
詞 一個代表在類別模組中宣告事件的「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
另請參閱
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。