finding out if a word object exitst

scratch

Registered User.
Local time
Today, 07:41
Joined
May 10, 2005
Messages
98
I want to find out if a specific object exists so I can close word on error.

Code:
Dim objWord As Word.Application
    
    'Set word as an application and make it invisible
    Set objWord = CreateObject("Word.Application")

What I want do is use objWord.Quit but if the object doesn't exist, objWord.Quit will cause the program to crash. Set objWord = Nothing doesn't work btw.

Thanks,
scratch
 
use the error catching.

Code:
dim ex as exception
try


catch ex

end try

or something like that.
 
Hi there,

This is a function I use to find out if the document exists:

Function Doc_exists(FName As String) As Boolean
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(FName) = True Then
Doc_exists= True
Else
Doc_exists= False
End If
End Function

Then use it in your code something like this:

If Doc_exists("Here the path and name of the document“) Then
Do something
Else
MsgBox "Document not found!", vbCritical, "Error"
Do something else
Exit Function
End If

Hope this helps
Marion
 
Hi,
If nothing of the above works, try using the Set objWord = nothing (yes, I know you said it doesn't work) but with this Dim instruction:

Dim objWord As Object
Set objWord = CreateObject("Word.Application")

It works for me... anyway, there are a lot more of things to consider, for instance:

1) If it's or isn't the only Word Application opened
2) If the Word Application has a Doc written or not when the error occurs...

If any of the solutions already given works, please give us more information about your project...
 

Users who are viewing this thread

Back
Top Bottom