Solved

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

Posted on 2016-08-23
10
54 Views
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
0
Comment
Question by:kasperEH
  • 5
  • 5
10 Comments
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
Fails on line 3 or 8?
0
 

Author Comment

by:kasperEH
Comment Utility
It fails on line 3, but under Advanced Properties I can see that "Kunde_adr1" is defined.
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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
0
 

Author Comment

by:kasperEH
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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)
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:kasperEH
Comment Utility
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?
0
 
LVL 14

Accepted Solution

by:
DrTribos earned 500 total points
Comment Utility
Google:  Application.Ontime

If needed I'll have time to provide an example later...
0
 

Author Comment

by:kasperEH
Comment Utility
Thank you, it works now
0
 

Author Closing Comment

by:kasperEH
Comment Utility
Thank you for your help.
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
Good work, glad to help :-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now