Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

867 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

18 Experts available now in Live!

Get 1:1 Help Now