Check For Valid Object Reference

G

George Lee

How do you check if a reference is still valid? For example, if at the time
you need to use a another document reference, how can you check that it’s
still valid?

Dim sourceDocument as document
Set sourceDocument = documents.add
…
If not sourceDocument is nothing then …

Here, the sourceDocument may have been closed before the last call, but in
checking the the reference, sourceDocument isn’t nothing (it’s still looks
valid), isn’t null (not really used much in VBA anyhow), or empty (it’s been
allocated at some point). Currently, I am forcing an error:

On Error Resume Next
Dim dummyString as string
dummyString = sourceDocument.Name
If Err.Number = 462 Then
Set sourceDocument = Nothing
End If

Is there a better way?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top