When I run this code, If Word is open I get the first message "A WORD DOCUMENT IS OPEN".…
- 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
- Assistant
The issue you are facing is because the
GetObjectfunction 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 theGetObjectfunction 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 closedBy rechecking if Word is still running using
GetObjectafter the first message, the code will exit the loop and continue with the rest of your code if Word is closed.