Delete empty rows in columns in Word

Microsoft Word 2003 win32(full prod)
February 21, 2010 at 12:40:09
Specs: Windows XP
I am merging documents from a database and in the document are many tables with rows and columns. In the end there are many tables that have empty rows (with more than one cell) because the database did not return any data. How can I run a script to go through the whole document and find empty rows (All the cells must be empty) and delete the whole row?

See More: Delete empty rows in columns in Word

Report •


#1
February 21, 2010 at 12:47:45
What database program are you using?

LIR


Report •

#2
February 21, 2010 at 13:06:03
Hi,

Are you trying to remove empty rows in tables in Word?

If so, do your tables have any merged cells.
In Word, tables cannot be easily accessed by Row or Column (in a macro) if there are merged cells.

If there are no merged cells then a macro can be written to remove rows where all cells are empty.

Regards


Report •

#3
February 21, 2010 at 13:24:46
The program I am using is a custom program that was written for attorneys - it is using sql at the background - the programmers have created all kind of extra screens and fields that you can use for document merging - problem is that somebody has 20 fields and they perhaps have just 10 fields that are filled in - the extra rows are then empty on the document, because I have to put them in the master if there is any information - the program was written in Clarion, but about that kind of program I do not know anything.

Report •

Related Solutions

#4
February 21, 2010 at 13:30:35
The rows are in different tables in a Word document. The headings for one table will be for example : Expenses and then the columns will be: Description, Reference, amount. Now on the master document I have to make provison if somebody fills in 20 fields. But in most instances only 5 or 10 are filled in. That leaves me with 10 or 15 rows that have to be deleted. In the document there are about 10 tables, and to delete them by hand is time consuming. I will love it if you can help me with a macro. Also important is that the documents name always change - depending on the file reference of the file.

Yes there are many rows that will have nothing in it because it refers to fields that have not been filled in. Those rows (containing more than one cell) that have nothing in any of the cells must be deleted.


Report •

#5
February 21, 2010 at 18:16:59
Hi,

I have a macro that will delete all empty rows in every table in a document.

As I said in my previous post, there is an issue with tables in MS Word that have merged cells. You did not confirm that there are no merged cells in the tables.

If there are no merged cells this macro should work.

Please test this macro on a copy of your data.
Note that there is no undo with macros and changes made with a macro cannot be undone with the Undo button.

If this works OK with your test data, you should always backup your data before running this.

The macro should be added to the Normal document so that it is available to different report documents.

From Word, hit Alt + f11 (the Alt key and function #11 key together).
This opens the Visual Basic (VB) window.
The Project Explorer pane should be visible on the left (if not click the View menu in the VB window and select Project explorer).
Right-click on 'Normal' and select Insert and Module (not Class module)
Double click on the new module (typically Module1)
Copy and paste the following into the large code pane in the VB window.

Option Explicit

Sub DelBlankRows()
Dim tblT As Table
Dim cllC As Cell
Dim blnEmpty As Boolean
Dim n As Integer

On Error GoTo ErrHnd

'loop through all tables in the document
For Each tblT In ActiveDocument.Tables()
    'loop through each row in table, starting at the end
    'of the table - so delete doesn't change row numbers
    For n = tblT.Rows.Count To 1 Step -1
        'set flag for empty row
        blnEmpty = True
            'go through each cell in row
            For Each cllC In tblT.Rows(n).Cells()
                'test if cell contains more than table marker
                If cllC.Range.Characters.Count > 1 Then
                    'if any cell has more than table marker
                    'the cell and therefore the row is not empty
                    blnEmpty = False
                End If
            Next cllC
            'if all cells in row empty - delete row
            If blnEmpty = True Then
                tblT.Rows(n).Delete
            End If
    Next n
Next tblT
Exit Sub
'error handler
ErrHnd:
Err.Clear
End Sub

Click File - Save from the VB menu

Use Alt + f11 to go back to the main Word window.
In the Word window, right click on a toolbar and select Customize
Select Commands, then Macros (scroll down to see this)
On the right side, your macro will be visible as Normal.Module1.DelBlankRows.
Click it and drag it onto a toolbar
Right-click the new 'button' and select 'Text only (In Menus)'
You will have a button (you can change or design a new icon image later).
Click close in the Customize dialog box.

Make the document with tables the active (selected) document, save as a copy.
Click the new button
The copy document should have all blank rows in tables deleted.

'Save as' final document if changes are acceptable.

Regards
PS if you have Word 2007, the steps for creating a button in the main Word window are different.


Report •

#6
February 21, 2010 at 21:24:13
Wow thanks a lot - the original master document has merged fields in, but if there is nothing in the database then the end result will be empty rows. Just one question - how do I create an icon in Word 2007 for this macro - thanks again from a sunny South Africa

Report •


Ask Question