PDA

View Full Version : Solved: Bold text below empty row before deleting empty row



clhare
01-11-2008, 06:51 AM
The following code deletes all empty rows in all the tables in a document. How would I adjust the macro if I want to bold the text in column 1 below the empty row before deleting the empty row?

Public Sub DeleteEmptyRows()
Dim oTable As Table
Dim oRow As Range
Dim oCell As Cell
Dim Counter As Long
Dim NumRows As Long
Dim TextInRow As Boolean
' Check all tables in document
For Each oTable In ActiveDocument.Tables
' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
For Counter = 1 To NumRows
StatusBar = "Row " & Counter
TextInRow = False
For Each oCell In oRow.Rows(1).Cells
If Len(oCell.Range.Text) > 2 Then
'end of cell marker is actually 2 characters
TextInRow = True
Exit For
End If
Next oCell
If TextInRow Then
Set oRow = oRow.Next(wdRow)
Else
oRow.Rows(1).Delete
End If
Next Counter
Next oTable
End Sub

fionabolt
01-11-2008, 09:59 AM
I have added just three lines to your original code:

1. set a variable for a new range,
Dim oRow2 As Range

2. When TextInRow returns false then you need to set the range oRow2 to the next row in advance of oRow
Set oRow2 = oRow.Next(wdRow)

3. and then apply bold formatting to that new range
oRow2.Rows(1).Cells(1).Range.Font.Bold = True


So the whole amended code looks like this:

Public Sub DeleteEmptyRows()
Dim oTable As Table
Dim oRow As Range
Dim oRow2 As Range
Dim oCell As Cell
Dim Counter As Long
Dim NumRows As Long
Dim TextInRow As Boolean
' Check all tables in document
For Each oTable In ActiveDocument.Tables
' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
For Counter = 1 To NumRows
StatusBar = "Row " & Counter
TextInRow = False
For Each oCell In oRow.Rows(1).Cells
If Len(oCell.Range.Text) > 2 Then
'end of cell marker is actually 2 characters
TextInRow = True
Exit For
End If
Next oCell
If TextInRow Then
Set oRow = oRow.Next(wdRow)
Else
'first set the range for the following row
Set oRow2 = oRow.Next(wdRow)
'then apply bold formatting to the first cell
oRow2.Rows(1).Cells(1).Range.Font.Bold = True
'then delete the empty row
oRow.Rows(1).Delete
End If
Next Counter
Next oTable
End Sub

clhare
01-11-2008, 10:21 AM
When I run the updated macro, I get an error that says "object variable or with block variable not set". I can't figure out what could be wrong.

TonyJollans
01-12-2008, 04:07 AM
That's because you are trying to bold the row after the last row in the table - add a check for being on the last row.

clhare
01-12-2008, 07:03 AM
I'm sorry... I don't understand what you mean.

TonyJollans
01-12-2008, 07:32 AM
Looking a bit more closely I see that your original code would fail if the last row of your table had text in it and the new version will fail if it is empty.

The reason is the use of oRow.Next(wdRow). When processing the last row of the table there isn't a Next Row. You need to add a check for being on the last row and, if so, not try to look at the next one.

clhare
01-12-2008, 08:01 AM
Ah... now I get it. Thank you! I will work on adding that last row check.

clhare
01-15-2008, 07:57 AM
Thank you for all your help! This macro now does everything I need it to do! :thumb

prasadavasar
03-10-2012, 07:37 AM
Hi,
If this thread is still open...I have gone through this post and its working fine in word....

I have created an excel macro file which create a seperate client letters in word...the master letter contain a table which has 7 rows. I want to delete the entire rows if any number out of 7 rows are blank. Note:The VBA coding is in excel...I want to delete the rows from word document.....your help would be greatly appriciated.