Link to home
Create AccountLog in
Microsoft Access

Microsoft Access

--

Questions

--

Followers

Top Experts

Avatar of jbotts
jbotts

VBA - From MS Access How to determine if MS Word is running.
Using MS Access 2003. I open a Word document, but need to know how to determine if MS Word is closed before closing a MS Access form. The Word application object is 'wo'. I get the run-time error as mentioned below.

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.


Avatar of danishanidanishani🇺🇸

Try to remove the Me. for the Function IsAppRunning.

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

Open in new window


Hope this helps,

Avatar of jbottsjbotts

ASKER

danishani:
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

Avatar of danishanidanishani🇺🇸

You need to add an ELSE statement, in case it return false (cq Instance not open):
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

Open in new window


Hope this helps,
Daniel

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of danishanidanishani🇺🇸

Make sure there is no Word Instance running by checking the Task Manager.
Sometimes the WORD instance is still in there, if its not closed propperly or 'hanging'.

Avatar of jbottsjbotts

ASKER

I checked and there is no Word instance listed in the Task Manager

Avatar of danishanidanishani🇺🇸

Hmm I run the code and it returns False when there is No word instance open and True when there is.

Did you copied the Funcion into a seperate Module?

Free T-shirt

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.


Avatar of mbizupmbizup🇰🇿

>> Set oApp = GetObject(, appName)

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

Open in new window


Avatar of jbottsjbotts

ASKER

The code is all in one module. I always get True, no matter if the Word application is running or not.

Avatar of mbizupmbizup🇰🇿

Before tesing the code I posted, make certain that there are no "hidden" instances of Word running from previous tests of this code.  You can do this through Task Manager, but the easiest way is to log out of your windows user account and log back in again (that will terminate any instances that may be running behind the scenes)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of jbottsjbotts

ASKER

mbizup:
I added the error handler and got the message "Error: 0".
jbotts

Avatar of jbottsjbotts

ASKER

mbizup:
I also logged out and then got the original error: "Run-time error '429': ActiveX component can't create object"

Avatar of mbizupmbizup🇰🇿

jbotts,

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 

Open in new window


Free T-shirt

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.


Avatar of mbizupmbizup🇰🇿

Did you copy/paste the entire function?

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




Avatar of jbottsjbotts

ASKER

mbizup:
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

Avatar of mbizupmbizup🇰🇿

Hmmm....  I've tested my code fairly extensively, and we are seeing very different results.

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.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of jbottsjbotts

ASKER

Function IsAppRunning(ByVal appName) As Boolean
    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


ASKER CERTIFIED SOLUTION
Avatar of danishanidanishani🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of mbizupmbizup🇰🇿

Okay - Looking at your code, I see where we miscommunicated.

< 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...

Avatar of mbizupmbizup🇰🇿

You want to get rid of that "On Error Resume Next" line in the code you just posted.  It is effectively cancelling out the custom error handling that we added.

Free T-shirt

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.


Avatar of jbottsjbotts

ASKER

Here is the code as it stands now:
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

Avatar of jbottsjbotts

ASKER

I also created the new database, and with no instance of Word, it gave me the message of "Word is open."

Avatar of mbizupmbizup🇰🇿

That definitely works for me -

ie:

I open Word, and it returns true; close Word and it returns False.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of mbizupmbizup🇰🇿

Are you seeing any instances of WinWord.Exe in Task manager?

Avatar of danishanidanishani🇺🇸

Same here, even with the original code, it works as aspected...

Avatar of jbottsjbotts

ASKER

I have rebooted my computer and still get the same result on the new database and on the original one.

Free T-shirt

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.


Avatar of jbottsjbotts

ASKER

I am working with MS Access 2003. Would that make a difference?

Avatar of danishanidanishani🇺🇸

No, Access version does not make a difference. Something very strange is going on.
Can you do the same test, and then change the instance into "Excel.Application".

See what message returns?

Avatar of mbizupmbizup🇰🇿

How exactly are you "killing" your Word Application objects?

>> 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

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of mbizupmbizup🇰🇿

Although it looks like you may already be doing both.

Avatar of jbottsjbotts

ASKER

I get the same result using Excel.Application. I am using Windows 7. Does that make a difference?

Avatar of mbizupmbizup🇰🇿

No - I'm testing this on a Windows 7 machine myself.

Free T-shirt

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.


Avatar of jbottsjbotts

ASKER

I do use objWord.Quit and then Set objWord = Nothing.

SOLUTION
Avatar of mbizupmbizup🇰🇿

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of mbizupmbizup🇰🇿

Sorry -

Those lines should all be:

? IsAppRunning("Word.Application")

Avatar of jbottsjbotts

ASKER

In the Immediate Window the function returns true under all circumstances.
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.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of mbizupmbizup🇰🇿

Agree with danishani that there is "something very strange going on", and I can't quite place a finger on it...

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.

Avatar of jbottsjbotts

ASKER

Thanks so much for the efforts mbizup and danishani. I will work on it a little longer.
jbotts

Avatar of mbizupmbizup🇰🇿

<In the Immediate Window the function returns true under all circumstances. >

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.

Free T-shirt

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.


Avatar of mbizupmbizup🇰🇿

Also try this from the immediate window in your new, empty Access Database:


?  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.

Avatar of peter57rpeter57r🇬🇧

Are you absolutely sure that there is no copy of Word running when you execute this command.
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.

Avatar of mbizupmbizup🇰🇿

Pete,

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?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of peter57rpeter57r🇬🇧

Well I did wonder whether he is using another product/application which itself uses Word as a component.  I think that is pretty much what you are asking also?
 
But I don't have any simple means of testing it.

Avatar of mbizupmbizup🇰🇿

<  I think that is pretty much what you are asking also?>

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.

Avatar of jbottsjbotts

ASKER

I have completed all the recommendations. The Task Manager did not list MS Word in Access or with Access not running. ?GetObject("Word.Application") in the immediate window returned "Microsoft Word". Using the following in the function I get the appropriate results:

   Set oApp = GetObject(, appName)
    If (Err.Number = 0) Then
        MsgBox "Word is open"
    Else
        MsgBox "Word is not open"
    End If

Free T-shirt

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.


Avatar of jbottsjbotts

ASKER

I retract that. Last night it worked. Now it doesn't.

Avatar of jbottsjbotts

ASKER

Now it does work. I had to log off and log back on before it worked. Will keep testing it and see if I can find why it works intermittently.

Avatar of jbottsjbotts

ASKER

I appreciate the time and learned something from both.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Microsoft Access

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.