View Full Version : Table has been deleted?
AAhrenst
09-03-2012, 12:38 PM
Hi,
I am having a problem with inserting tables into a word document. Problem is not consistent as I am updating and inserting about 30 tables successfully in the same document. However, from time to time I will get following Error code: "2825 object has been deleted". Seems to be occurring a the same few table which keep me wondering....
What I am doing is basically following for each table in separate procedures:
Dim TempTable as word.table
If ActiveDocument.Bookmarks.Exists(Table_BM) = True Then
Selection.GoTo What:=wdGoToBookmark, Name:=Table_BM
If Selection.Information(wdWithInTable) = True Then
ActiveDocument.Bookmarks(Table_BM).Range.Tables(1).Delete
end if
else
'do something else
end if
'insert new table
Set TempTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=5, NumColumns:=5)
ActiveDocument.Bookmarks.Add Range:=TempTable.Cell(1, 1).Range, Name:=Table_BM
'update new table below...
Error occurs after the new table was been inserted when trying to insert the bookmark. Error has nothing to do with the bookmark as I could not even delete the table again if I wanted i.e. Selection.Tables(1).delete.
Table does not exist despite it has been clearly created. Table does look somewhat unusual as first row is always 3 lines high.
Any suggestions as to what is happening here will be much appreciated before I lose all my hair...:(
Thanks
AAhrenst
fumei
09-03-2012, 04:58 PM
Well I am not getting that. What I am getting is a Variable not defined error, as Table_BM in the code must be "Table_BM". It requires the quotes.
If I add the quotes the code executes fine. It deletes the existing table/bookmark; makes a new table (5 x 5) and makes the first cell (and ONLY the first cell) a new bookmark - also named "Table_BM".
AAhrenst
09-03-2012, 05:43 PM
Thanks for the reply.
Well the code is working fine as I am already using same to update many other tables. The bookmark you are referring to is a variable and hence does not required the quotes. However, for your execution purposes yes.
Anyway the error has nothing to do with the bookmark as the table is not properly defined somehow. I can tell during debugging.
If I edit the word document manually while debugging I will need to place the cursor right above the table and press ENTER. This will make the table below look correct again and I will be able to continue running the code.
I know it does not make much sence but that is why I am asking this forum for help...I am sure someone have had the same problem or have an idea of what could cause this...
thanks
fumei
09-03-2012, 07:58 PM
You are using a bookmark variable? That is odd.
I am missing something. Are you saying the code works for SOME tables, but not others??
What do you mean "edit the document while debugging"?
There seems to be divergent questions. On one hand you seem to have a problem with the table being deleted (your subject line), on the other the new table - even though inserted as 5 x 5 - is NOT 5 x 5 ("the table below look correct ").
Is this correct?
Can you post a document for us to look at?
Tinbendr
09-04-2012, 07:16 AM
If you are doing this from Excel, then you should specify the selection.
Set TempTable = ActiveDocument.Tables.Add(Range:=Word.Selection.Range, NumRows:=5, NumColumns:=5) The VBA may not know which Selection you refering to: Excel Selection or Word Selection.
The same goes for Activedocument if there is a chance you will have more than one document open at a time.
AAhrenst
09-04-2012, 08:28 AM
Thanks for the feedback.
I have attached a document for better explanation although I cannot attach the actual file. It seems to me that the issue might start from before the old tables is being deleted and the new table is updated.
Could it be that some tables losses its definition and needs to be reactivated? Anyhow, I get the error message right after having generated the new table. See the attachment for better explanation.
Tinbendr, I dont have excel open but I have tried to add "word." but it didn't make any difference:
Set TempTable = Word.ActiveDocument.Tables.Add(Range:=Word.Selection.Range, NumRows:=5, NumColumns:=5)
ActiveDocument.Bookmarks.Add Range:=TempTable.Cell(2, 1).Range, Name:=BookmarkName
fumei, the new table is always generated from what appears to be 5x5. Again in the attachment you will see what I mean when I say the first row looks a little different. I am not sure this is important...it is just an observation.
Again, this problems only appears from time to time. When it occurs it seems to be several tables with same problem. If I keep updating (or deleting the tables completely an regenerated them) after 10 - 20 time the problem will go away again. I am working with a big template that will be distributed to a lot of people. Until I understand this issue I cannot release the document.
thanks
fumei
09-04-2012, 11:08 AM
I got rid of 2010 (hate it), so I can not actually open the file - just view it. So hopefully someone else can help you.
It is possible there is some corruption issue.
"Could it be that some tables losses its definition and needs to be reactivated?"
No, that is not possible. There is no reactivation property in tables, and if a table "lost its definition" then I would think there is definitely a corruption issue.
"I get the error message right after having generated the new table." This does not make any sense. There is no delay in error messages. If you get an error stating object is deleted, then there must be some instruction attempting to do something with that object.
Could you please post your entire code? All you have is a snippet, and there must be something there that is getting that error.
AAhrenst
09-04-2012, 02:02 PM
Hello again,
I have written a piece of code to get around the problem. What I am doing is simply to place the cursor above the table and type enter followed by backspace. If I do this I will not be able to reproduce the error. I am most definately not an expert on programming and this is surely not the best solution...but is gets the job done albeit a bit moneysome.
This aside, I just realized that the problems goes away every time I reopen the template. So, I will not be able to send the corrupted table unless I send the whole document which unfortunately I am unable to share.
Secondly, I really dont like to have a code dependent on selection.moveXX which in my opinion is bound to produce an error sooner or later...Is there a method of moving directly to the line above selected table/object?
Again, if anyone can think of a reason why this would happen and/or any suggestion what else to try to avoid it, it would be much appreciated.
Thanks again
'************************************************************************** ***********************
'****** FUNCTION BELOW IS ONLY USED BECASUE OF A BUG IN WORD. ******
'****** FROM TIME TO TIME A TABLE WILL LOOSES ITS "DEFINITION" WHICH IS STILL NOT UNDERSTOOD. ******
'****** BY PLACING THE CURSOR ABOVE THE TABLE AND HIT ENTER FOLLOWED BY DELETION OF SAME LINE ******
'****** ..THE PROBLEM WILL DISSAPEAR AGAIN. THIS IS DONE FOR EVERY TABLE BEFORE DELETION. ******
'************************************************************************** ***********************
Private Function EnsureTableIntegrity(Table_BM) As Boolean
Dim i As Integer
i = 0
Selection.GoTo What:=wdGoToBookmark, Name:=Table_BM
Do
i = i + 1
Selection.MoveUp Unit:=wdLine, Count:=1
Loop Until Selection.Information(wdWithInTable) = False Or i = 50
If Selection.Information(wdWithInTable) Or i = 50 Then
EnsureTableIntegrity = False
Else
Selection.HomeKey Unit:=wdLine
Selection.TypeParagraph
Selection.TypeBackspace
EnsureTableIntegrity = True
End If
Selection.GoTo What:=wdGoToBookmark, Name:=Table_BM
End Function
gmaxey
09-04-2012, 02:31 PM
Can you send me a website feedback, I'll reply and you can send your document.
fumei
09-05-2012, 11:06 AM
Frankly, I am unconvinced there actually IS a bug. You still won't post your code? You do not have to post a document, but surely there can be no objection to posting what you are actually doing.
BTW: if I were you I would take up Greg's offer. He knows his stuff.
Frosty
09-05-2012, 11:23 AM
Well, I can see where you might be creating a problem, depending on how your bookmark is defined with regards to the table and your use of the .Information object. And why are you using the Selection object at all?
If what you want is to find a table which contains the bookmark, and then delete it, why don't you just do that?
Sub Demo
Dim sBookmarkName As String
sBookmarkName = "Test"
If ActiveDocument.Bookmarks.Exists(sBookmarkName) Then
If ActiveDocument.Bookmarks(sBookmarkName).Range.Tables.Count = 1 Then
ActiveDocument.Bookmarks(sBookmarkName).Range.Tables(1).Delete
End If
End If
End Sub
Does that work to delete the table in all cases?
If so, then we can figure out the best way for you to store the range at which you want to insert the new one. But you need to break apart the functionality. Because the problem probably exists in the chunk of code you're not showing (since you insert the table based on your selection, and you check the selection, etc etc-- get rid of your use of the selection, and simplify the process)
Frosty
09-05-2012, 11:28 AM
And by the way, using the Selection object in code, and then manipulating the document (i.e., the Selection object) while debugging your code can lead to lots of unexpected behavior.
Off the top of my head--
1. Your selection could look like it's after a table, but it's actually just before the end of the table marker... trying to manipulate that range is going to cause problems.
2. Your selection could be right next to another table, which could cause an issue when you then create a new table at that spot (i.e., while your temptable variable may seem like it's handling one table, you might have caused word to merge the table with something).
3. Your selection could be somewhere "bad"
4. You could be encountering a real bug. This last scenario is the least likely since, as you say, you are a neophyte programmer. The most likely scenario is that you are causing your own problem.
fumei
09-05-2012, 05:27 PM
Unless we can see the actual real code (in full), I suspect that #4 is likely, although #1 is quite possible.
Further I am not entirely sure it s a table with a bookmark 9although that IS what happens with the NEW table); it could be a bookmark that contains a table...which is what I thought was the situation. After all it is:
ActiveDocument.Bookmarks(Table_BM).Range.Tables(1).Delete
i.e. a table object within the range of the bookmark. Not the other way aound. But...we just do not know for sure.
Frosty
09-05-2012, 05:29 PM
Actually, a table which contains a bookmark named "test" will be selected in the entirety with the following line of code, even if the bookmark doesn't span the whole table.
ActiveDocument.Bookmarks("test").Range.Tables(1).Select
Frosty
09-05-2012, 05:30 PM
But there are strange effects using .Delete on tables/rows/columns/cells, depending on how it is done. Sometimes you might only be clearing out the text, other times you might be actually deleting the object.
fumei
09-05-2012, 05:44 PM
There is no Selection going on (at least in the original code), but yes, true, this also applies to .Delete.
Which is (I think) significant, in that once the .Delete executes, the Selection is moved back one character. Which also brings up your comment about using Selection and possible wonky effects.
AAhrenst
09-05-2012, 10:32 PM
Thanks for all the feedback.
I wanted to reply earlier but I have been spending some time looking into the problem a bit further. I which I could attached the document but instead i have tried to rebuild an example of what is going on. See attached docm.
I have narrow the error down to involving 3 tables.. each of which is updated using same routines. Tables 1, 2, 3 are placed in order. Table 1 and 3 are simple tables can can be replaced with same simple routines as i have attached. Table two is actually a series of tables with nested tables included.
If Table two breaks into a second page during update the error will occur later on when table 1 is updated. As the error is occurring with table 3 it get really hairy trying to figure out what is really going on. Anyway, If I remove the nested tables the problem will dissapear.
So I looked into the routines I used for setting up the tables. If I remove the following line the error will disappear:
TableName.Rows.AllowBreakAcrossPages = False
I though it could have something with the way I am nesting the tables. I have tried both of following with same results. Both gives a nesting level 2 for all nested tables:
InsertRange.Collapse Direction:=wdCollapseStart
Set TempTable = ActiveDocument.Tables.Add(Range:=InsertRange, NumRows:=5,
'or
TempTable.Range.Cut
NewTable.Cell(x, y).Range.PasteAsNestedTable
Bug or not, I like the idea of marker position before deletion as a table should be possible to delete. The routine I am using is ensuring that the table is present and that no other bookmarks is in the table before deletion. It also ensures that no other tables are present above or below which I can also verify visually. Marker position is always same location as the deleted table. However, is there such think as a "bad" location which cannot be captured visually?
Below is the process for generating the tables and deleting the tables:
Public Sub CreateXXXTable()
UpdateFeedback = UpdateTableBookmark("TableBookmark","TableName")
If UpdateFeedback(1) = "False" Then
Exit Sub
End If
Call MessageLog("Updating " & TableName & " Table")
Set TempTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=5, NumColumns:=5)
ActiveDocument.Bookmarks.Add Range:=TempTable.Cell(1, 1).Range, Name:=UpdateFeedback(2)
With TempTable
'update content
End with
Public Function UpdateTableBookmark(Table_BM As String, TableName As String) As String()
If CheckBookmarkPosition(Table_BM, TableName) And NoMultipleBookmarksInTable(Table_BM) Then
Selection.GoTo What:=wdGoToBookmark, Name:=Table_BM
ActiveDocument.Bookmarks(Table_BM).Range.Tables(1).Delete
GoNoGo(1) = "True"
GoNoGo(2) = Table_BM
Else
GoNoGo(1) = "False"
End If
'other options....
gmaxey
09-06-2012, 04:37 AM
I ran your code a dozen or more time with and without your troublesome line and I could not duplicate your error. Using a range object over the selection should speed it up a bit.
Public Sub Showbug()
Dim MainTable As Table
Dim TempTable As Table
Dim TableAbove As Table
Dim TableBelow As Table
Dim TempCell As Cell
Dim oRng As Word.Range
'Table Above
Set oRng = ActiveDocument.Bookmarks("TableAbove").Range
oRng.Tables(1).Delete
Set TableAbove = ActiveDocument.Tables.Add(Range:=oRng, NumRows:=1, NumColumns:=2)
'ActiveDocument.Bookmarks("TableAbove").Range.Tables(1).Select
'ActiveDocument.Bookmarks("TableAbove").Range.Tables(1).Delete
'Set TableAbove = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:=2)
ActiveDocument.Bookmarks.Add Range:=TableAbove.Cell(1, 1).Range, Name:="TableAbove"
Call SetTableStyle(TableAbove, 10, 7)
For i = 1 To 10
TableAbove.Rows.Add
Next i
'Table causing error
Set oRng = ActiveDocument.Bookmarks("MainTable").Range
oRng.Tables(1).Delete
Set MainTable = ActiveDocument.Tables.Add(Range:=oRng, NumRows:=25, NumColumns:=2)
'ActiveDocument.Bookmarks("MainTable").Range.Tables(1).Select
'ActiveDocument.Bookmarks("MainTable").Range.Tables(1).Delete
'Set MainTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=12, NumColumns:=2)
ActiveDocument.Bookmarks.Add Range:=MainTable.Cell(1, 1).Range, Name:="MainTable"
Call SetTableStyle(MainTable, 10, 7)
For Each TempCell In MainTable.Range.Cells
Set oRng = TempCell.Range
'TempCell.Select
'Selection.Collapse Direction:=wdCollapseStart
oRng.Collapse wdCollapseStart
'Set TempTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=2, NumColumns:=2)
Set TempTable = ActiveDocument.Tables.Add(oRng, 2, 2)
Call SetTableStyle(TempTable, 10, 3.5)
With TempTable
.Cell(1, 1).Range = "I"
.Cell(1, 2).Range = "can't"
.Cell(2, 1).Range = "reproduce"
.Cell(2, 2).Range = "error"
End With
Next TempCell
'Table Below with error
Set oRng = ActiveDocument.Bookmarks("TableBelow").Range
oRng.Tables(1).Delete
Set TableBelow = ActiveDocument.Tables.Add(Range:=oRng, NumRows:=4, NumColumns:=2)
'ActiveDocument.Bookmarks("TableBelow").Range.Tables(1).Select
'ActiveDocument.Bookmarks("TableBelow").Range.Tables(1).Delete
'Set TableBelow = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=4, NumColumns:=2)
ActiveDocument.Bookmarks.Add Range:=TableBelow.Cell(1, 1).Range, Name:="TableBelow"
Call SetTableStyle(TableBelow, 10, 7)
End Sub
Frosty
09-06-2012, 06:51 AM
Nested tables can be an issue. Really don't recommend using them if at all possible.
It sounds like some of your tables are simply corrupt. How long have the original documents been around? Converted from word perfect? Data pasted in from other programs?
You should understand the distinction between corruption and a bug. They are quite different. Bugs are problems that (generally) can be solved, by being avoided. Corruption is simply a time bomb in your document. Corruption can not be solved via code. Code can facilitate the *recreation* of a corrupt area of the document, but it cannot reliably deal with a corrupt area of the document.
Also: your code snippets and Greg's example should be a good lesson.
1. Giving snippets of code only show is part of a problem that you are unable to solve. This handcuffs us in trying to solve your problem. You should really post an entire routine. It does not have to be your original routine. A sample routine which recreates the problem is great. But Sub AND End Sub is needed
2. Sample document. Again, you describing the problem is inefficient. Imagine if a homicide detective wasn't allowed to actually go to the scene of the crime, but could only interview people in the surrounding apartments? We all appreciate the need for sensitive data to stay off the Internet, but posting a sample (as you did, now) is really the only way to have the problem solved.
I haven't looked at your attachment yet, but if you can work out whether it is corruption or a bug, that would be really useful. Can you actually cause the problem in your attached document?
If you can and Greg cannot, can you try Start > Run > Winword / a
That starts a clean winword process with no addins. Then see of your attached document still causes the issue. If it does, let us know.
Frosty
09-06-2012, 06:56 AM
To be more succinct: I think you may be treating a document corruption issue as if it is a bug. If it is corruption, spending time treating it like a bug is inefficient. So I would focus on really identifying the problem in a sample document and a clean winword process
Frosty
09-06-2012, 10:04 AM
As a follow up... I downloaded your attachment, and ran the Showbug routine and got zero errors, no matter whether I had both lines in SetTableStyle commented, uncommented, or either one commented/uncommented.
So I have performed 4 tests while running Showbug repeatedly on your .docm attachment.
1. .AllowPageBreaks = False commented out, .Range.ParagraphFormat.KeepWithNext = True commented out
2. .AllowPageBreaks = False uncommented, .Range.ParagraphFormat.KeepWithNext = True commented out
3. .AllowPageBreaks = False commented out, .Range.ParagraphFormat.KeepWithNext = True uncommented
4. .AllowPageBreaks = False uncommented, .Range.ParagraphFormat.KeepWithNext = True uncommented
If the Showbug routine does actually cause an error on your system in at least one of the tests above, then try those same tests when you open the document in a "clean" Winword process (achieved by Start > Run > winword /a)
If it causes a problem on your system in the clean winword process, then you need to a) tell us what version of Word you're working in and b) make sure you have all the latest service packs/hotfixes.
I have concerns about the efficiency of your code and why you're using nested tables, but they really don't matter in terms of solving this problem. You have to identify whether you are encountering an actual bug, or if you're just dealing with document corruption. The solution will be quite a bit different, depending on that determination.
Those are the next steps. Hope this helps.
fumei
09-06-2012, 12:34 PM
Well I have asked for fuller code twice, but I am being ignored.
Ooooo, nested tables now is it? That can be an issue.
I remain unconvinced it is a real bug.
I will let Greg and Jason (Frosty) handle this - together they are the best of the best here - as it may be a 2010 issue (but I doubt that).
Frosty
09-06-2012, 01:07 PM
Fumei,
The OP actually did try to provide the full code in the attachment. But running that code requires being able to open the document, because he has tables constructed in it as well, which the routines use. I could, of course, work up a sample for you-- but all it would demonstrate is that there is no bug, at least as presented.
Like you, I have my issues with nested tables-- but these nested tables are actually not causing a problem (at least, not yet). I question the *need* for nested tables at all, I would never want to take a buggy object (like tables) and then start nesting buggy objects within it. Grin.
But I think it's likely to be a document corruption issue...
AAhrenst
09-06-2012, 02:21 PM
Thanks for all the feedback
Actually I have importing my codes into a new document and it does the same thing. All my document does is updating tables so If I open a new document and instert tables with certain bookmarks it will update whatever is there.
Anyway, I will try to attached another document which does the error consistently.
thanks
Frosty
09-06-2012, 02:23 PM
Sorry, AAhrenst... but I don't understand. You aren't having the error in the document you attached previously?
Before you attach another document, make sure it causes the problem in a "winword /a" created process too, please.
And what version of Word are you using?
nihao15
09-10-2012, 02:35 AM
And by the way, using the Selection object in code, and then manipulating the document (i.e., the Selection object) while debugging your code can lead to lots of unexpected behavior.
Off the top of my head--
1. Your selection could look like it's after a table, but it's actually just before the end of the table marker... trying to manipulate that range is going to cause problems.
2. Your selection could be right next to another table, which could cause an issue when you then create a new table at that spot (i.e., while your temptable variable may seem like it's handling one table, you might have caused word to merge the table with something).
3. Your selection could be somewhere "bad"
4. You could be encountering a real bug. This last scenario is the least likely since, as you say, you are a neophyte programmer. The most likely scenario is that you are causing your own problem.
this is cool!!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.