Word 2003: Run Time Error 5825 - Object Deleted

A

Ac-Deucy

Below is some VBA code from a Word 2003 macro (not the entire subroutine).
The ActiveDocument.Variable("Document Number") is defined in an earlier
subroutine, and the user is prompted to enter it along with other document
variables. Unfortunately, if the user leaves it blank, the above error
message results. Debug shows that processing stops at the line

If VProcNum = "" Then

Trying some differentl things with this IF statement, VProcNum does not
indicate to be NULL, ISERROR, or ISEMPTY. How do I fix the macro to prevent
this error message? Here's the macro...

Sub ManualTitle()
'
' ManualTitle Macro
'
Dim VProcNum
'
' Create variable for Procedure Number; if it is empty, warn the user
'
Set VProcNum = ActiveDocument.Variables("Document Number")
If VProcNum = "" Then
Style = vbOKOnly + vbInformation + vbDefaultButton1
Response = MsgBox("Without a Document Number, some header variables
will not be populated ", Style, "No Document Number")
End If
 
J

Jezebel

The problem is the use of 'set' in this line --

Set VProcNum = ActiveDocument.Variables("Document Number")

Because you've declared VProcNum as a variant, this line assigns VProcNum
to the doc variable *object* -- not the actual value that the object
contains. If the variable is defined, then the following references to
VProcNum still work, because the value is the default property of that
object. Currently when you refer to VProcNum, you are actually using
VProcNum.Value

Better coding would be along these lines --

Dim VProcNum as string

'Retrieve the variable, ignoring the error if it doesn't exist
On error resume next
VProcNum = ActiveDocument.Variables("Document Number")
On error goto 0

If len(VProcNum) = 0 then 'More efficient way to check if the
string is empty
....
 

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