MS Access VBA检查Word是否已关闭?

时间:2016-05-10 09:29:13

标签: vba ms-access ms-word access-vba

我有一个项目要求我在Word中打开文档并在文档打开时等待。然后,用户需要编辑文档,打印或保存文档,然后手动关闭文档。当文档(或Word本身)关闭时,我想在Access中继续我的VBA脚本。

我有以下代码

If Len(Dir(sDocName)) > 0 Then
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document

    ' Launch Word
    Set wordApp = New Word.Application

    ' Open the document
    With wordApp
        Set wordDoc = .Documents.Open(sDocName, , False)

        ' Pass data
        With wordDoc
            .Variables("bmArchitectCompanyName").Value = "Hello"
            .Variables("bmArchitectCompanyAddress").Value = "Hello"
            .Variables("bmArchitectCompanyPostCode").Value = "Hello"
            .Variables("bmArchitectContactFirstName").Value = "Hello"
            .Variables("bmProjectTitle").Value = ProjectTitle
            .Fields.Update
        End With
        .Visible = True
        .Activate

        Do Until wordApp Is Nothing
            ' Wait for Word to be closed
        Loop

        ' Display a success message
        MsgBox "Success!"
    End With
End If

然而,do循环永远不会退出。

如何检查用户是否通过VBA代码关闭了通过VBA启动的文档?

6 个答案:

答案 0 :(得分:0)

您必须使用DoEvents屈服于操作系统,然后检查文档是否已关闭,否则将继续执行DoEvents循环。以下是我遗憾无法测试的大纲代码。

    Do 
        DoEvents
        For i = 1 to wordApp.Documents.Count
            If (wordApp.Documents(i).Name = sDocName) Then Exit For
        Next i
        If (i > wordApp.Documents.Count) Then Exit Loop
    Loop While (True)

注意:您可能不想关闭应用程序,因为用户可能打开了其他文档。也许你应该测试一下。

答案 1 :(得分:0)

我找到了解决方案......

Do While .Visible
       ' wait until the window is no longer visible       
Loop

我正在寻找的是什么,等待word关闭然后继续使用MS Access VBA脚本。

答案 2 :(得分:0)

以保罗输入的内容为基础,使用不同的方法来测试文档是否已打开。

Sub Test()

    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Dim sDocName As String

    sDocName = "<Full path to document>"

    Set wordApp = New Word.Application

    With wordApp
        Set wordDoc = .Documents.Open(sDocName, , False)
        .Visible = True
        .Activate

        Do
            DoEvents
        Loop While FileIsOpen(sDocName)

    End With

    MsgBox "Finished"

End Sub

Public Function FileIsOpen(FullFilePath As String) As Boolean

    Dim ff As Long

    On Error Resume Next

    ff = FreeFile()
    Open FullFilePath For Input Lock Read As #ff
    Close ff
    FileIsOpen = (Err.Number <> 0)

    On Error GoTo 0

End Function

答案 3 :(得分:0)

检查wordApp Is Nothing是否是VBA在外部关闭时不知道取消引用该对象的问题。换句话说,当wordApp引用的文档关闭时,指向对象的指针变为伪造但不是Nothing。要捕获事件,您可以对循环中的文档对象进行无意义的调用,并在发生错误时退出:

On Error Resume Next
With wordDoc
  Do
    DoEvents
    'This will only occur if there is an error:
    If .Visible<>.Visible Then Exit Do
  Loop
End With
Err.Clear
On Error Goto 0

答案 4 :(得分:0)

我刚试过没有循环或其他任何东西。 Access中的宏代码等待,直到Word应用程序的实例在完成执行之前关闭。这是我测试的内容,而MsgBox仅在我退出Word后执行:

' Open the document
With wordApp
    Set wordDoc = .Documents.Add

    ' Pass data
    With wordDoc
        .Variables("bmArchitectCompanyName").Value = "Hello"
        .Variables("bmArchitectCompanyAddress").Value = "Hello"
        .Variables("bmArchitectCompanyPostCode").Value = "Hello"
        .Variables("bmArchitectContactFirstName").Value = "Hello"
        .Content.Text = "test"
        '.Variables("bmProjectTitle").Value = ProjectTitle
        '.Fields.Update
    End With
    .Visible = True
    .Activate

    ' Display a success message
    MsgBox "Success!"
    Set wordDoc = Nothing
    Set wordApp = Nothing
End With
  

请注意,您希望确保将对象设置为Nothing或   您下次运行代码时可能会收到错误消息   因为它会让WinWord的实例在内存中打开。

答案 5 :(得分:0)

如果您正在处理类模块或表单模块中的Word文档,请尝试以这种方式声明Word.Document对象:

Dim WithEvents w As Word.Document

然后您可以使用Word.Document对象事件“关闭”:

Private Sub w_Close()

   'Things you want to do when the Word.Document is closed

   'reset object
   set w=nothing

End Sub

除非w为空,否则你不能让“容器”形式关闭或对象终止。在您的Word文档关闭之前,表单或对象是安静的(或者您希望他们做什么)。