Search code examples
vbams-wordoffice365

How to remove line within tables?


I'm trying to remove a line/lines before some images that are inside a row in more than one table in document.

The part of the table looks like this and line above the images is shown by the black arrow.

enter image description here

enter image description here

This is my current code but is not removing anything

Sub RemoveLine()
Dim oTable As Table
Dim oRng As Range
Dim oFind As Range
   
Application.ScreenUpdating = False
    For Each oTable In ActiveDocument.Tables
    
        Set oRng = oTable.Range
        Set oFind = oTable.Range
        
        With oRng.Find
            Do While .Execute(FindText:="[^13^l]{1,}", MatchWildcards:=True)
                If oFind.InRange(oRng) Then
                    oFind.Text = ""
                End If
            Loop
        End With

    Next oTable

Application.ScreenUpdating = True
Set oTable = Nothing
Set oRng = Nothing

End Sub

Udpate

After run code of @taller, it deletes in some cases and doesn´t delete in other cases. I see the variable sText in both cases and looks like below in each case. This is a sample file.

enter image description here


Solution

    • Loop through all cells and remove the nested table located at the top of each cell range.

    • Note: Backup your file before testing.

    Sub RemoveNestedTable()
        Dim mainTable As Table, oCell As Cell
        Dim cellRange As Range, sText As String, i As Long
        If ActiveDocument.Tables.Count > 0 Then
            ' loop through table
            For Each mainTable In ActiveDocument.Tables
                ' loop through cells
                For Each oCell In mainTable.Range.Cells
                    Set cellRange = oCell.Range
                    sText = cellRange.Text
                    If Left(sText, 5) = Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(47) Then
                        cellRange.Collapse Direction:=wdCollapseStart
                        ' nested table
                        If cellRange.Tables.Count > 0 Then
                            cellRange.Tables(1).Delete
                        End If
                    End If
                Next
            Next
        End If
    End Sub
    

    enter image description here


    Question: if there is another way to identify only the nested table that is especifically at the top of the cell?

    Sub NestedTab()
        Dim t As Table, tt As Table
        Dim c As cell
        For Each t In ActiveDocument.Tables
            For Each c In t.Range.Cells
                If c.Tables.Count > 0 Then
                    For Each tt In c.Tables
                        If tt.Range.Start = c.Range.Start Then
                            tt.Delete
                        End If
                    Next tt
                End If
            Next c
        Next t
    End Sub