Application.NewDocument 事件 (Word)

會在建立新文件時發生。

注意事項

如果您正在使用內嵌在另一份文件中的文件,則不會發生此事件。

語法

私人子應用程式 _NewDocument (ByVal 文件 做為文件)

運算式 變數,代表已使用類別模組中的事件宣告的 Application 物件。

參數

名稱 必要/選用 資料類型 描述
Doc 必要 Document 新文件。

註解

如需搭配 Application 物件使用事件的詳細資訊,請參閱 搭配 Application 物件使用事件。

範例

本範例會詢問使用者是否要建立新的文件時儲存所有其他開啟的文件。 此程式碼必須放置在類別模組中,並且必須正確初始化該類別的執行個體才能看到此範例工作;如需如何完成這項操作的指示,請參閱搭配 Application 物件使用 事件。

Public WithEvents appWord as Word.Application 
 
Private Sub appWord_NewDocument(ByVal Doc As Document) 
 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 支援與意見反應。