Solved

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

Posted on 2004-04-28
6
1,454 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

840 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