Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

When I run this code, If Word is open I get the first message "A WORD DOCUMENT IS OPEN".…

  1. User

    When I run this code, If Word is open I get the first message "A WORD DOCUMENT IS OPEN". If I do not close the Word document, I get the second message "WORD IS STILL OPEN" If I close Word after the second message, the VBA exits the loop and continues with the rest of my other code. Which is what is expected. However, If Word is open and I get the first message "A WORD DOCUMENT IS OPEN". If I close Word at this point, I still get the second message "A WORD DOCUMENT IS OPEN" and the VBA exits the loop and continues with the rest of my other code. I would expect that after the first message, if I close Word, I should not get the second message "A WORD DOCUMENT IS OPEN". Can you help me fix this.

    ’ Check to see if Word is open Dim wdApp As Object Dim wdRunning As Boolean Dim wdTimeout As Date

    On Error Resume Next Set wdApp = GetObject(, “Word.Application”)

    If Err.Number = 0 Then wdRunning = True wdTimeout = Now + TimeValue(“00:00:10”) ’ Set timeout duration here (10 seconds in this example) Else wdRunning = False End If

    If wdRunning Then MsgBox “A WORD DOCUMENT IS OPEN” & vbCrLf & vbCrLf & _ "First close Word then click OK here to continue ", vbCritical Do While Now < wdTimeout wdRunning = Not wdApp Is Nothing If Not wdRunning Then Exit Do wdApp.WaitUntilIdle DoEvents ’ Allow VBA to respond to events Loop If wdRunning Then MsgBox “WORD IS STILL OPEN” & vbCrLf & vbCrLf & _ “Please close it and click OK here to continue.”, vbCritical End If End If

    ’ VBA continues when Word is finally closed

  2. Assistant

    The issue you are facing is because the GetObject function is not updated when you close Word after the first message. To fix this, you can modify the code to recheck if Word is still running after the first message by using the GetObject function again. Here's the modified code:

    ' Check to see if Word is open
    Dim wdApp As Object
    Dim wdRunning As Boolean
    Dim wdTimeout As Date
    
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    
    If Err.Number = 0 Then
        wdRunning = True
        wdTimeout = Now + TimeValue("00:00:10") ' Set timeout duration here (10 seconds in this example)
    Else
        wdRunning = False
    End If
    
    If wdRunning Then
        MsgBox "A WORD DOCUMENT IS OPEN" & vbCrLf & vbCrLf & _
               "First close Word then click OK here to continue ", vbCritical
        Set wdApp = Nothing ' Release the previous reference to Word Object
        Do While Now < wdTimeout
            Set wdApp = GetObject(, "Word.Application") ' Recheck if Word is still running
            wdRunning = Not wdApp Is Nothing
            If Not wdRunning Then Exit Do
            wdApp.WaitUntilIdle
            DoEvents ' Allow VBA to respond to events
        Loop
        If wdRunning Then
            MsgBox "WORD IS STILL OPEN" & vbCrLf & vbCrLf & _
                   "Please close it and click OK here to continue.", vbCritical
            Set wdApp = Nothing ' Release the reference again before continuing
        End If
    End If
    
    ' VBA continues when Word is finally closed
    

    By rechecking if Word is still running using GetObject after the first message, the code will exit the loop and continue with the rest of your code if Word is closed.