View Full Version : [SOLVED:] Determining if a table has been deleted in Word
Digilee
09-28-2012, 12:18 AM
I have an application that reformats certain tables in a Word document to our company standard. I must be able to work on documents that have tracked changes that have not been accepted. I need to determine if a table has been deleted so that I do not process it further.
I tried using the x.Range.Revisions(1).Type method (x = table being checked), but a wdRevisionDelete can refer to the fact that the last revision was a character/word/etc. deletion as well as indicating that the table itself has been deleted. Even if I check the Revisions.Count = 1 (deleting a table sets the count to 1 no matter how many previous revisions), that still is not enough; I will get the same count if just 1 character was deleted in the table.
Help. Please.
Lee
macropod
09-28-2012, 01:52 AM
What you can test is the end-of-row and the end-of cell markers. If the last end-of-table marker hasn't been deleted, neither has the table been. However, if the last row has been deleted, so will that marker have been, so then you'd need to test the individual end-of-cell markers (if you know no tables have vertically-merged cells, you could just test the end-of-row markers for all rows), with code like;
Sub TableTest()
Dim Tbl As Table, TblCell As Cell, bDel As Boolean
For Each Tbl In ActiveDocument.Tables
With Tbl.Range.Characters.Last
If .Revisions.Count > 0 Then
If .Revisions(.Revisions.Count).Type = wdRevisionDelete Then
bDel = True
For Each TblCell In Tbl.Range.Cells
With TblCell.Range.Characters.Last
If .Revisions.Count = 0 Then
bDel = False
Exit For
End If
End With
Next
If bDel = True Then MsgBox "Table has been deleted"
End If
End If
End With
Next
End Sub
Digilee
09-28-2012, 11:18 AM
Hi Paul,
Thanks for the help. I had been hoping that there would be a specific table element revision that I could check to see if the table itself were deleted. I took your code and modified it for my needs as shown below. The comments are for my benefit to make sure I understand what I am doing. Am I close?
Regards,
Lee
Function IsTableDeleted(thisTable As Table) As Boolean
Dim TblCell As Cell, bDel As Boolean
' If only one revision, there's a chance it is a deletion
' of the table, otherwise, it can't be. Don't care what
' type the revision is at this point.
IsTableDeleted = False
If thisTable.Range.Revisions.Count <> 1 Then Exit Function
' Is the table deleted, or is it just a single revision somewhere in the table?
With thisTable.Range.Characters.Last
' If the last character in the table has no revisions,
' the table can't be deleted, so get out (quick test).
' Again, we don't care at this point about the revision type
If .Revisions.Count > 0 Then
' OK, the last character in the table has a revision, but
' it could just be the one character that was actually
' deleted, so check the last character in each cell
' for revisions. If the table is NOT deleted, the odds are
' that a non-revised last cell character will be found
' fairly quickly and we can get out fast. If the table
' is truely deleted, we'll have to cycle through all the
' cells.
If .Revisions(.Revisions.Count).Type = wdRevisionDelete Then
bDel = True
For Each TblCell In thisTable.Range.Cells
With TblCell.Range.Characters.Last
If .Revisions.Count = 0 Then
bDel = False
Exit For
End If
End With
Next
If bDel = True Then IsTableDeleted = True
End If
End If
End With
End Function
macropod
09-29-2012, 12:28 AM
I'd do and express it this way:
Function IsTableDeleted(thisTable As Table) As Boolean
Dim Tbl As Table, TblCell As Cell, bDel As Boolean
With thisTable.Range.Characters.Last
bDel = True
'If the last character has no revisions,
' the table is present.
If .Revisions.Count = 0 Then
bDel = False
ElseIf .Revisions(.Revisions.Count).Type = wdRevisionDelete Then
'Since the last Character has been deleted,
' we need to check whether any
' other cells are present.
For Each TblCell In Tbl.Range.Cells
With TblCell.Range.Characters.Last
'If the last character has no revisions,
' the table is present.
If .Revisions.Count = 0 Then
bDel = False
Exit For
'If the last character revision is not a deletion,
' the table is present.
ElseIf .Revisions(.Revisions.Count).Type <> wdRevisionDelete Then
bDel = False
Exit For
End If
End With
Next
Else
'Since the last character revision is not a deletion,
' the table is present.
bDel = False
End If
End With
IsTableDeleted = bDel
End Function
This should ensure you function always returns a valid result.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.