Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-28
6
Medium Priority
?
1,573 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

715 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