We help IT Professionals succeed at work.

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

kasperEH asked
Last Modified: 2016-08-25
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
Watch Question

Fails on line 3 or 8?


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

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

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


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.



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)


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?
This one is on us!
(Get your first solution completely free - no credit card required)


Thank you, it works now


Thank you for your help.

Good work, glad to help :-)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.