A family of Microsoft word processing software products for creating web, email, and print documents.
The output is okay, that's normal.
Another try with the code below, please.
Andreas.
Option Explicit
Sub Main()
DeleteEmptyRowsInTable ThisDocument.Tables(1)
End Sub
Sub DeleteEmptyRowsInTable(ByVal T As Table)
'Delete all empyt rows in the given table, can contain vertical and horizontal merged cells
Dim C As Cell
Dim R As Integer, i As Integer
Dim Stack() As Integer
ReDim Stack(0 To 0) As Integer
With T
'The FOR EACH visits the cells in ascending order
R = 1
For Each C In .Range.Cells
'Is the current row greater then our row pointer?
If C.RowIndex > R Then
'Yes, we can delete the last row, save the row
ReDim Preserve Stack(0 To UBound(Stack) + 1)
Stack(UBound(Stack)) = R
'Go one with the next row
R = R + 1
End If
'Must we check this row?
If C.RowIndex = R Then
'Skip this row if the cell is not empty
If Len(TrimWhite(C.Range.Text)) > 0 Then R = R + 1
End If
Next
'Delete all empty rows from bottom to top
For R = UBound(Stack) To 1 Step -1
'Note:
' .Rows(Stack(r)).Delete
'raises a RTE 5991 if the table contains vertical merged cells
'.Cell.Split
Set C = Nothing
For i = 1 To .Columns.Count
Set C = GetCell(T, Stack(R), i)
If Not C Is Nothing Then Exit For
Next
If Not C Is Nothing Then
C.Select
Selection.Rows.Delete
End If
Next
End With
End Sub
Private Function TrimWhite(ByVal S As String) As String
'Return a string with white space removed
Dim i As Long
For i = 1 To Len(S)
If Asc(Mid$(S, i, 1)) < 32 Then Mid$(S, i, 1) = vbNullChar
Next
TrimWhite = Replace$(S, vbNullChar, "")
End Function
Private Function GetCell(ByVal T As Table, ByVal Row As Long, ByVal Column As Long) As Cell
On Error Resume Next
Set GetCell = T.Cell(Row, Column)
End Function