codiplex
codiplex

Reputation: 159

VBA: Run-time error '5825' Object has been deleted

I was putting this code ActiveDocument.Variables("time1").Delete before End Sub and I get that error "Object has been deleted" so that if a variable "time1" exist, it will be deleted at the end of the procedure. I understand why I get that code because "time1" has already deleted on the first run but I want to skip and end the sub if it encounter errors. I tried to do this

On Error Goto here
ActiveDocument.Variables("time1").Delete

here:
End sub

but I still get that Error. Why is it that the error handler did not work?

Upvotes: 1

Views: 6372

Answers (1)

user1379931
user1379931

Reputation:

You should be able to avoid this if you are not interested in whether the Variable existed or not.

ActiveDocument.Variables("time1") = ""

should remove a Variable called "time" if there is one, and execute without errors if there is not.

In a similar way,

ActiveDocument.Variables("time1") = "something"

will create the Variable if it does not exist.

This is one of the things that makes Variables slightly easier to work with than Custom Document Properties, although it does mean that empty variables are not allowed.

Upvotes: 1

Related Questions