run-time error 5828 Object has been deleted

M

mikenam

I get a random Run-time error '5825' Object has been deleted

there are 5 tables all with 4 columns. The program runs through each
table and deletes all columns that have List or Adjust as a header. I
test with the same tables each time but i get error at different tables
each time. Running the program with 1 table is fine but multiple tables
the error pops up.

This line is the line that i get the error. Seems like it can't find
the table after it edits it correctly.
MsgBox IsObjectValid(oTb)
f = oTb.Columns.count



For Each oTb In ActiveDocument.Tables
tbl = tbl + 1
'if there are 4 columns then format the table
If oTb.Columns.count = 4 Then
i = 1
f = oTb.Columns.count
Do

test = Left(oTb.Cell(1, i).Range.Text, 4)
If test = "List" Or test = "Adju" Then
oTb.Columns(i).Delete
i = i
Else
i = i + 1
End If
MsgBox IsObjectValid(oTb)
f = oTb.Columns.count

Loop Until i > f
End If
 
J

Jean-Guy Marcil

mikenam was telling us:
mikenam nous racontait que :
I get a random Run-time error '5825' Object has been deleted

there are 5 tables all with 4 columns. The program runs through each
table and deletes all columns that have List or Adjust as a header. I
test with the same tables each time but i get error at different
tables each time. Running the program with 1 table is fine but
multiple tables the error pops up.

This line is the line that i get the error. Seems like it can't find
the table after it edits it correctly.
MsgBox IsObjectValid(oTb)
f = oTb.Columns.count



For Each oTb In ActiveDocument.Tables
tbl = tbl + 1
'if there are 4 columns then format the table
If oTb.Columns.count = 4 Then
i = 1
f = oTb.Columns.count
Do

test = Left(oTb.Cell(1, i).Range.Text, 4)
If test = "List" Or test = "Adju" Then
oTb.Columns(i).Delete
i = i
Else
i = i + 1
End If
MsgBox IsObjectValid(oTb)
f = oTb.Columns.count

Loop Until i > f
End If

Imam not sure abnoput your code... it looks strange to me!
What does
tbl = tbl + 1
do?
What is
IsObjectValid(oTb)
???

In any case, when conditionally deleting items from a collection, it is
usually preferable, if possible, to start from the end and work your way to
the beginning, so you don't get nasty surprises because the counter is too
high after having deleted a few items...
 
M

mikenam

tbl shouldn't be there. isobjectvalid returns true or false if oTb is a
valid object. I was using it to see why i get the error. Thanks Jean
im still pretty new at this. Ill give it a try.
 
M

Manfred F

Hi,
iterating a collection and conditionally deleting its members is always kind
of a difficult action, as deleting interferes with iteration.

I use two different ways to get around this:
a) iterate through the collection backwards, starting with the last element.
for i = collection.count to 1 step -1 do
....
next i
b) (preferred) iterate through the collection, keep the elements to delete
in a separate collection and delete them afterwards
for each element in collection
if deletionCondition(element) then deleteCollection.Add element
next element

for each element in deleteCollection
element.delete
next element

Kind Regards

Manfred
 
T

Tony Jollans

I think a little more detail is needed. When you get the error and,
presumably, see False returned IsObjectValid, does the table still exist, or
have all its columns been deleted? What are the tables that you use all the
time like, and what is the expected result?
 
M

mikenam

yes the table still exist but with 2 columns instead of 4. The thing i
don't understand is the problem does not occur at the same time. it
will never throw an error for the first table. the second table there
might be an error there might not be an error. same with the third
table and so on but never the first table. It is odd that it happens
randomly.
 
T

Tony Jollans

I can't find any way to duplicate the error - although I do notice some odd
aspects of the display sometimes in 2002 and 2003 but not in 2007. What
version of Word are you using?
 
D

Doug Robbins - Word MVP

Tony,

Per a post in the vba.beginners newsgroup, he is using Word 2003.
 

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