PDA

View Full Version : Checking if a object is valid



Adamski
05-13-2010, 09:32 AM
How do you check if a object is still valid?
For example:
You create a word instance
Later you want to check it hasn't been closed

Dim ObjWord As Word.Application
Set ObjWord = CreateObject("Word.Application")

...

If not ObjWord Is Nothing then

If ObjWord has been closed:
it is not nothing
it is not null
it is not empty

Trying to use it you get the error:
"The remote server machine does not exist or is unavailable"

Is there a way to test it is valid other than error trapping?

TonyJollans
05-13-2010, 11:21 AM
Maybe I'm showing my ignorance, but I think trapping the error is the easiest way.

Paul_Hossler
05-13-2010, 07:04 PM
I do something like this to try and keep it under control.


Sub drv()
Dim ObjWord As Object
Set ObjWord = CreateObject("Word.Application")

ObjWord.Application.Visible = True
If ObjWord.Name = "Microsoft Word" Then MsgBox "Word Open"

Call ObjWord.Quit
Set ObjWord = Nothing ' sort of workaround

If ObjWord Is Nothing Then MsgBox "Word Closed"

End Sub


I think I've seen something about testing objects somewhere. I'll have to look

Paul

Adamski
05-14-2010, 01:51 AM
Paul, that is fine if I'm closing in the code - But I need to check if it is still valid when it may have been closed another way (User, Other Running Code, etc).

Cheers

Paul_Hossler
05-14-2010, 11:07 AM
Ah Well -- Tony (as always) has the best way

Paul

fumei
05-17-2010, 09:56 AM
Let's be clear here.

" I need to check if it is still valid when it may have been closed another way "

Then you mention - User.

While the application may be closed by the user, th euser can NOT destroy the instance, the:

Set ObjWord = CreateObject("Word.Application")

Even if the user closes Word (clicking the X for example), THAT instance, that allocated memory still exists.

Application.Quit (the equivalent to user clicking the X)

is NOT the same as:

Set objWord = Nothing

So what, exactly, are you asking about? The application or the instance?


How do you check if a object is still valid?
For example:
You create a word instance
Later you want to check it hasn't been closed
Instance are not closed...applications are.

Adamski
05-18-2010, 02:42 AM
Closing the Application.
I have used error trapping when trying to use the Object again which is working fine - just wondered if there was some other way.

fumei
05-18-2010, 08:54 AM
Nope.