Microsoft Access
--
Questions
--
Followers
Top Experts
Private Sub cmdbCloseCC_Click()
On Error GoTo PROC_ERR
Dim wn As String
wn = "Word.Application"
If Me.IsAppRunning(wn) = True Then
wo.Quit
Set wo = Nothing
End If
DoCmd.Close acForm, "frmCC_Status"
Application.Quit
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox Err.Description
Resume PROC_EXIT
End Sub
__________________________
Function IsAppRunning(ByVal appName) As Boolean
Dim oApp As Object
On Error Resume Next
Set oApp = GetObject(, appName) 'Error here - Run-time error '429': ActiveX component can't create object
Debug.Print oApp
If Not oApp Is Nothing Then
Set oApp = Nothing
IsAppRunning = True
End If
End Function
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
So change this into this:
Private Sub cmdbCloseCC_Click()
On Error GoTo PROC_ERR
Dim wn As String
wn = "Word.Application"
If IsAppRunning(wn) = True Then ' without the Me.
wo.Quit
Set wo = Nothing
End If
DoCmd.Close acForm, "frmCC_Status"
Application.Quit
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox Err.Description
Resume PROC_EXIT
End Sub
Hope this helps,
The return of IsAppRunning(wn) is now True and should be False as there is no Word application running. The wo.Quit is for a modular variable which is a Word.Application.
Thanks
On Error GoTo PROC_ERR
Dim wn As String
wn = "Word.Application"
If IsAppRunning(wn) = True Then
wo.Quit
Set wo = Nothing
Else
' instance is not open
Msgbox "Application is closed."
End If
DoCmd.Close acForm, "frmCC_Status"
Application.Quit
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox Err.Description
Resume PROC_EXIT
End Sub
Hope this helps,
Daniel






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Sometimes the WORD instance is still in there, if its not closed propperly or 'hanging'.
Did you copied the Funcion into a seperate Module?

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
GetObject will return error 429 if there is no instance of that application to "get" (ie: if there is no word application open).
To get the results you want, you need to handle error 429. If it is raised by GetObject, there is no application running. If it is not raised by GetObject, there IS an application running to 'get'.
This should do the trick:
Function IsAppRunning(ByVal appName) As Boolean
Dim oApp As Object
On Error GoTo EH
Set oApp = GetObject(, appName) 'Error here - Run-time error '429': ActiveX component can't create object
Debug.Print "oApp:" & oApp
If Not oApp Is Nothing Then
Set oApp = Nothing
IsAppRunning = True
End If
Exit Function
EH:
If Err.Number = 429 Then
' there is no Word Application open to 'get'
IsAppRunning = False
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I added the error handler and got the message "Error: 0".
jbotts
I also logged out and then got the original error: "Run-time error '429': ActiveX component can't create object"
I believe you can also streamline the code I posted a bit. GetObject will either return error 439, or it will return an Object, so with the handling for error 429 in place you don't need the check for "= Nothing". You should be able to reduce the code to this:
Function IsAppRunning(ByVal appName) As Boolean
Dim oApp As Object
On Error GoTo EH
Set oApp = GetObject(, appName) 'Error here - Run-time error '429': ActiveX component can't create object
Debug.Print "oApp:" & oApp
Set oApp = Nothing
IsAppRunning = True
Exit Function
EH:
If Err.Number = 429 Then
' there is no Word Application open to 'get'
IsAppRunning = False
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
These lines are critical for the error handling to work:
>>> On Error GoTo EH
We don't want to resume to the next line
>>> Exit Function
We want to exit normally if no errors are encountered.
Also, make sure that you have set error handling to "Break on unhandled errors"
Do this from the VB Editor:
Tools --> Options --> general --> Error Trapping
I did the above. Without an instance of Word, IsAppRunning still returns True. Before I posted the question, I did copy and paste the whole function.
jsbotts
To make sure we are thinking on the same page, go ahead and post your function exactly as you are using it now, with my suggested modifications.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Dim oApp As Object
On Error GoTo EH
On Error Resume Next
Set oApp = GetObject(, appName) 'Error appears here
Debug.Print oApp
Set oApp = Nothing
IsAppRunning = True
Exit Function
EH:
If Err.Number = 429 Then
' there is no Word Application open to 'get'
IsAppRunning = False
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function
< I did copy and paste the whole function.>
My intent in that question was to ask whether you copy/pasted the function exactly as *I* had posted it.
There is more to it than just the error handling.
Try this again:
1. Log out of your windows account to kill any hidden Word processes.
2. Copy/paste and run the function exactly as posted in either of my comments
Let me know the results...

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Function IsAppRunning(ByVal appName) As Boolean
Dim oApp As Object
On Error GoTo EH
Set oApp = GetObject(, appName) 'Error appears here
Debug.Print oApp
Set oApp = Nothing
IsAppRunning = True
Exit Function
EH:
If Err.Number = 429 Then
' there is no Word Application open to 'get'
IsAppRunning = False
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function
ie:
I open Word, and it returns true; close Word and it returns False.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Can you do the same test, and then change the instance into "Excel.Application".
See what message returns?
>> Set objWord = Nothing
Just setting it to nothing doesnt necessarily do the trick if you create an oblect in code; it just clears the memory taken up by the object to my understanding.
To fully kill a word (or Excel or anything else ) application, you need to do something like this
objWord.Quit '<--- Closes Word
Set objWord = Nothing '<-- Frees the object variable






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Those lines should all be:
? IsAppRunning("Word.Applica
I did a 'Debug.Print oApp' in the function. When an instance of Word is open oApp is Microsoft Word, and when there is not an instance of Word oApp is evidently Null as nothing is printed.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I think dani and I are both seeing 'expected results'.
I need to get some sleep, but will check back in the morning to see whether this has gotten any further.
jbotts
Just verifying... You did try danishani's suggestion to place the function in a brand new database with nothing else, and the results were the same, correct?
Try this --
1. In a new windows login session Ctrl-alt-del, bring up Task Manager. Are there any instances of Microsoft Word showing in Task Manager under the Applications tab, or "WINWORD.EXE" under the processes tab?
2. Open a brand new database in Access 2003. Open Task Manager again, and repeat the same checks with Access open.
Post back with your findings.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
? GetObject(, "word.application")
What does that print in the debug window (or does it give you an error) ?
Also, if possible, try this function on a different computer.
I don't know how you have been checking, but you should use Ctrl-Alt-del to get into Task Manager and sort the processes alphabetically; look for WinWord.
I'm wondering if he doesn't have an Add-In for Word running. Do you think that could cause the behavior he's seeing?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
But I don't have any simple means of testing it.
Yes - I'm not sure how to conclusively test for it either.
___
One other thought here... If I'm understanding this correctly, the goal in using this function is to close out Word objects that were opened through Access code. I'm not sure that this is the best approach. If Word is open it doesn't necessarily prove that the object/instance opened through the application's code was left open.
Maybe a better check would be to test whether objWord (or whatever it is named) has a value.
Set oApp = GetObject(, appName)
If (Err.Number = 0) Then
MsgBox "Word is open"
Else
MsgBox "Word is not open"
End If

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Microsoft Access
--
Questions
--
Followers
Top Experts
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.