DocumentOpen 事件

       

该事件在文档打开时发生。

Private Sub object_DocumentOpen(ByVal Doc As Document)

object   在类模块的事件中声明的 Application 类型对象。有关使用 Application 对象的详细内容,请参阅使用 Application 对象事件

Doc   将打开的文档。

示例

本示例在文档打开时,提示用户是否要保存所有其他已打开的文档。 为了看到本例的执行结果,本例提供的代码必须保存在一个类模块中,而且该类中的实例必须正确初始化;有关完成上述任务的指导,请参阅使用 Application 对象事件

Public WithEvents appWord as Word.Application

Private Sub appWord_DocumentOpen(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