Testing A Document's Validity

G

George Lee

If the user closes a document, the object reference becomes invalid but it is
not NULL, Nothing, or Empty. How do test if a document is still valid? For
example:

dim gWordDocument as Word.Document
Set gWordDocument = ActiveDocument
...
'If the user closes the document now, the following tests do not work
If IsEmpty(gWordDocument) Then ...
If IsNull(gWordDocument) Then ...
If gWordDocument Is Nothing Then...

'This will error
Label3.Caption = "File name: " & gWordDocument.Windows(1).Caption
 
J

Jezebel

Trap the error --

Dim pTemp as string

:
on error resume next
pTemp = gWordDocument.Name
if err.Number <> 0 then
set gWordDocument = Nothing 'Doc has been closed
end if
on error goto 0
 
Top