ceidelman
asked on
Cannot select rows in VBA when a Microsoft Word table contains merged cells
Hi
This is my code to delete row(s) in a Word table if the row contains certain text.
Sub DeleteBlankTableRows()
Dim deleted As Boolean
Dim oRow As Row
deleted = False
On Error Resume Next
ActiveDocument.Tables(1).S elect
If Selection.Information(wdWi thInTable) = False Then
MsgBox "No table exists in the document!", vbCritical, "Error"
Exit Sub
End If
For Each oRow In Selection.Tables(1).Rows
If oRow Is Nothing Then
MsgBox "The tool cannot work in this table. This might be because one or more rows have merged cells. If these merged cells are removed, it will probably work.", _
vbCritical, "Error"
End
End If
'Or InStr(oRow.Cells(2).Range. Text, "*To Be Deleted*") > 0 Then
If InStr(oRow.Range.Text, "*To Be Deleted*") > 0 Then
oRow.Delete
deleted = True
End If
Next oRow
If deleted Then
MsgBox "All Blank lines have been deleted.", vbOKOnly, "Success!"
Else
MsgBox "No Blank lines containing *To Be Deleted* text can be found.", vbOKOnly, "Failure!"
End If
End Sub
If there are merged cells however, error 5991 is generated at the following line:
For Each oRow In Selection.Tables(1).Rows
Any ideas?
Thanks
This is my code to delete row(s) in a Word table if the row contains certain text.
Sub DeleteBlankTableRows()
Dim deleted As Boolean
Dim oRow As Row
deleted = False
On Error Resume Next
ActiveDocument.Tables(1).S
If Selection.Information(wdWi
MsgBox "No table exists in the document!", vbCritical, "Error"
Exit Sub
End If
For Each oRow In Selection.Tables(1).Rows
If oRow Is Nothing Then
MsgBox "The tool cannot work in this table. This might be because one or more rows have merged cells. If these merged cells are removed, it will probably work.", _
vbCritical, "Error"
End
End If
'Or InStr(oRow.Cells(2).Range.
If InStr(oRow.Range.Text, "*To Be Deleted*") > 0 Then
oRow.Delete
deleted = True
End If
Next oRow
If deleted Then
MsgBox "All Blank lines have been deleted.", vbOKOnly, "Success!"
Else
MsgBox "No Blank lines containing *To Be Deleted* text can be found.", vbOKOnly, "Failure!"
End If
End Sub
If there are merged cells however, error 5991 is generated at the following line:
For Each oRow In Selection.Tables(1).Rows
Any ideas?
Thanks
Hi ceidelman,
Just ran your code in Word 2000, table with merged cells across columns ran fine, table with merged cells across dows tripped your error handler for "The tool cannot work in this table...". I also cannot reproduce the error 5991.
Just ran your code in Word 2000, table with merged cells across columns ran fine, table with merged cells across dows tripped your error handler for "The tool cannot work in this table...". I also cannot reproduce the error 5991.
ASKER
Hi. The error I'm trapping (one that lpzcoville mentioned) is the error 5991. I was hoping there is a way to work around it in the first place, because my code cannot delete the rows if the table anywhere contains merged cells.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You da man! It will be my pleasure to give you these points.
Thanks!
Thanks!
I cannot reproduce on Office XP