In this VBA code, The loop does not exit when I close Word: ' Check to see if Word is…
- User
In this VBA code, The loop does not exit when I close Word: ' Check to see if Word is open Dim wdApp As Object Dim wdRunning As Boolean Dim wdLoop As Boolean On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err.Number = 0 Then wdRunning = True 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 wdLoop = True Else wdLoop = False End If Do While wdLoop wdApp.WaitUntilIdle wdRunning = wdApp.IsRunning If Not wdRunning Then wdLoop = False Else MsgBox "WORD IS STILL OPEN" & vbCrLf & vbCrLf & _ "Please close it and click OK here to continue.", vbCritical End If Loop ' VBA continues when Word is finaly closed
- Assistant
The issue with this code is that the loop does not have any condition that would cause it to exit naturally. It relies on the user closing Word to exit the loop.
To fix this, you can add a condition to check if Word is still running inside the loop, and exit the loop when it is no longer running. Here is an updated version of the code:
' Check to see if Word is open Dim wdApp As Object Dim wdRunning As Boolean On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err.Number = 0 Then wdRunning = True 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 wdApp Is Nothing Set wdApp = GetObject(, "Word.Application") If wdApp Is Nothing Then MsgBox "WORD IS STILL OPEN" & vbCrLf & vbCrLf & _ "Please close it and click OK here to continue.", vbCritical End If Loop End If ' VBA continues when Word is finally closedThis code checks if Word is open at the beginning, and if it is, it enters a loop that continuously checks if Word is still running. If it is running, it displays a message prompting the user to close Word. Once Word is closed, the loop will exit naturally and VBA will continue executing the remaining code.
- User
The loop exists even if Word is still open
- Assistant
Apologies for the confusion. To ensure the loop exits only when Word is closed, you can modify the code as follows:
' Check to see if Word is open Dim wdApp As Object Dim wdRunning As Boolean On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err.Number = 0 Then wdRunning = True 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 wdRunning wdApp.WaitUntilIdle wdRunning = wdApp Is Nothing If wdRunning Then MsgBox "WORD IS STILL OPEN" & vbCrLf & vbCrLf & _ "Please close it and click OK here to continue.", vbCritical End If Loop End If ' VBA continues when Word is finally closedIn this updated code, the loop checks if
wdAppis stillNothingto determine if Word is still running. IfwdAppisNothing, it means Word has been closed and the loop will exit. The loop also includes a call towdApp.WaitUntilIdleto ensure proper synchronization between VBA and Word. - User
The loop still exists if the open Word document is still open in the background and not closed
- Assistant
Apologies for the confusion once again. If the loop is not exiting when the Word document is open in the background, you can modify the code to check for open documents in Word and prompt the user to close all the documents. Here's an updated version of the code:
' Check to see if Word is open Dim wdApp As Object Dim wdRunning As Boolean Dim wdDocs As Object On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err.Number = 0 Then wdRunning = True Set wdDocs = wdApp.Documents Else wdRunning = False End If If wdRunning Then MsgBox "A WORD DOCUMENT IS OPEN" & vbCrLf & vbCrLf & _ "First close all open Word documents then click OK here to continue ", vbCritical Do While wdDocs.Count > 0 MsgBox "WORD DOCUMENTS ARE STILL OPEN" & vbCrLf & vbCrLf & _ "Please close all open Word documents and click OK here to continue.", vbCritical Set wdDocs = wdApp.Documents Loop End If ' VBA continues when all Word documents are finally closedIn this updated code, after verifying that Word is running and obtaining a reference to the collection, the loop checks if there are any open documents in Word (). If there are open documents, it prompts the user to close them and waits for the user's action. Once all documents are closed, the loop will exit and the VBA code will continue executing.
- User
When I close word, the loop does not exit and I am stuck with a Message Box open
- Assistant
Apologies for the inconvenience. In that case, you can modify the code to include a timeout mechanism that will exit the loop after a certain duration if Word is still open.
Here's an updated version of the code that includes a timeout mechanism:
' 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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.