Avatar of kasperEH
kasperEH
Flag for Denmark asked on

Word VBA code fails with 'Object has been deleted' error

I have created a Word macro that works fine when run manually, but when I run it at startup for a template in a Document_New sub it fails:
Private Sub Document_New()
Dim zipCity As String
zipCity = ActiveDocument.Variables("Kunde_adr1")
Dim lenVar As Integer
lenVar = Len(zipCity)
Dim city As String
city = Mid(zipCity, 9, lenVar)
ActiveDocument.Bookmarks("bimCity").Range.Text = city
End Sub

Open in new window

The error is Run-time error '5825': Object has been deleted

Please help me fix this
Microsoft WordVBA

Avatar of undefined
Last Comment
DrTribos

8/22/2022 - Mon
DrTribos

Fails on line 3 or 8?
kasperEH

ASKER
It fails on line 3, but under Advanced Properties I can see that "Kunde_adr1" is defined.
DrTribos

Sure, but might be the case that "ActiveDocument" refers to a document you don't expect OR that the variables are not loaded.

You can add
doEvents

Open in new window

above line 3, or implement some other delay to check.

Also, you can use
debug.print ActiveDocument.Name

Open in new window

to see if you got the document you expect.  The output of debug.print will appear in the Immediate Window Ctrl + G to show / Hide
Your help has saved me hundreds of hours of internet surfing.
fblack61
kasperEH

ASKER
I have added the two lines, but I get the same error. ActiveDocument is the current document, the one I am creating. That has been confirmed by the debug.print statement.
DrTribos

Well....

debug.print
ActiveDocument.Variables.Count

Open in new window

and if there is more than 0 you could try a small  loop to list them, something like:
For iCount = 1 to ActiveDocument.Variables.Count
debug.print ActiveDocument.Variables(iCount).Name
Next iCount

Open in new window


(Not Tested)
kasperEH

ASKER
Sorry for the delay. I have tried it now and count of variables is 0. So it probably has something to do with the order things are loaded. Can I execute something after everything is loaded?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
DrTribos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
kasperEH

ASKER
Thank you, it works now
kasperEH

ASKER
Thank you for your help.
DrTribos

Good work, glad to help :-)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy