Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-28
6
Medium Priority
?
1,607 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
5 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

879 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