Solved

Cannot select rows in VBA when a Microsoft Word table contains merged cells

Posted on 2004-04-28
6
1,385 Views
Last Modified: 2008-03-17
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).Select
If Selection.Information(wdWithInTable) = 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
0
Comment
Question by:ceidelman
  • 2
  • 2
6 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 10942220
What is an error 5991?

I cannot reproduce on Office XP
0
 
LVL 4

Expert Comment

by:lpzCoville
ID: 10942357
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.  

0
 

Author Comment

by:ceidelman
ID: 10943167
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.
0
 
LVL 4

Accepted Solution

by:
lpzCoville earned 125 total points
ID: 10943571
try this:

    With Selection.Tables(1).Range
        For Each oCell In .Cells
            With oCell.Range
                If InStr(.Text, "*To Be Deleted*") Then .Rows.Delete
            End With
        Next
    End With


downside:  from the (short) tests tried, i think that this will delete the cells all the rows across which a vertically merged cell spans, even if the vertically merged cell does not contain the *delete* message.
0
 

Author Comment

by:ceidelman
ID: 10943814
You da man! It will be my pleasure to give you these points.

Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now