In this VBA event below, If a word document is open it triggers a first message "A WORD…
- User
In this VBA event below, If a word document is open it triggers a first message "A WORD DOCUMENT IS OPEN". When the word document is closed the VBA should then exit the loop and proceed without any other message. If the word document is not closed after the first message the vba should remain in the loop and it should trigger a second message "WORD IS STILL OPEN". When the word document is eventually closed the VBA should then exit the loop and proceed without any other message. The VBA should not exit the loop until the word document is closed. The issue I am having with this VBA event is; When I get the first message "A WORD DOCUMENT IS OPEN" and I then close the word document, I still get the second message "WORD IS STILL OPEN" before the VBA exits the loop. Can you fix this issue.
' Check to see if Word is open Dim wdApp As Object Dim wdRunning As Boolean Dim wdTimeout As Date Application.EnableEvents = False On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err.Number = 0 Then wdRunning = True wdTimeout = Now + TimeValue("00:00:05") ' Set timeout duration here (5 seconds in this example) Else wdRunning = False End If If wdRunning Then MsgBox "A WORD DOCUMENT IS OPEN" & vbCrLf & vbCrLf & _ "First Save/Close Word then click OK here to continue ", vbCritical 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 Save/Close Word and click OK here to continue.", vbCritical Set wdApp = Nothing ' Release the reference again before continuing End If End If Application.EnableEvents = True ' VBA continues when Word is finally closed
- Assistant
To fix the issue, you can add another condition in the second message part to check whether the Word document is still open or not. If it is closed, then the VBA will exit the loop without showing the second message. Here's the modified code:
' Check to see if Word is open Dim wdApp As Object Dim wdRunning As Boolean Dim wdTimeout As Date Application.EnableEvents = False On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err.Number = 0 Then wdRunning = True wdTimeout = Now + TimeValue("00:00:05") ' Set timeout duration here (5 seconds in this example) Else wdRunning = False End If If wdRunning Then MsgBox "A WORD DOCUMENT IS OPEN" & vbCrLf & vbCrLf & _ "First Save/Close Word then click OK here to continue ", vbCritical 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 Set wdApp = GetObject(, "Word.Application") ' Recheck if Word is still running after first message wdRunning = Not wdApp Is Nothing If wdRunning Then MsgBox "WORD IS STILL OPEN" & vbCrLf & vbCrLf & _ "Please Save/Close Word and click OK here to continue.", vbCritical Set wdApp = Nothing ' Release the reference again before continuing End If End If End If Application.EnableEvents = True ' VBA continues when Word is finally closed
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.