Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-08-23
10
Medium Priority
?
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:DrTribos
ID: 41767774
Fails on line 3 or 8?
0
 

Author Comment

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

Expert Comment

by:DrTribos
ID: 41768349
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:kasperEH
ID: 41768357
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 15

Expert Comment

by:DrTribos
ID: 41768469
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
 

Author Comment

by:kasperEH
ID: 41769925
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 15

Accepted Solution

by:
DrTribos earned 2000 total points
ID: 41769990
Google:  Application.Ontime

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

Author Comment

by:kasperEH
ID: 41770103
Thank you, it works now
0
 

Author Closing Comment

by:kasperEH
ID: 41770106
Thank you for your help.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 41770163
Good work, glad to help :-)
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

604 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