Deleting Blank Rows in a Word Table

D

Doctorjones_md

I have a word Template with several Tables on it. I need code which will
examine all tables in the Template and do the following:

1. Delete any empty rows in each table
2. Delete any Table having Only a Header Row

How would I go about writing this code?

Thanks in advance for any help with this request.
 
J

Jean-Guy Marcil

Doctorjones_md was telling us:
Doctorjones_md nous racontait que :
I have a word Template with several Tables on it. I need code which
will examine all tables in the Template and do the following:

1. Delete any empty rows in each table
2. Delete any Table having Only a Header Row

How would I go about writing this code?

Thanks in advance for any help with this request.

Define "empty"....

Would spaces or nothing but consecutive ¶ count as empty?


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Doctorjones_md

Jean,

Thanks for the reply :) SEE NOTE at the end of this entry ...

Empty (in this instance) meaning -- Say I have several tables in my
template, and from a From, the user will make various selections which will
populate rows in some (but not all) of the tables. I need code which will
examine these tables and delete any rows having not data (ie -- no data
populated by the Forms)
========================================
Jean -- this is a continuation of the project/issue you helped me with back
in Nov -- I just need to clean up the empty rows now (see your original code
below)

Something like this (combining the code from above):
'_______________________________________
Sub DelTablesAndAddSum()

Const strBookRoot As String = "Table"
Const strCode1 As String = "=SUM("
Const strCode2 As String = ") \# ""#,##0.00;- #,##0.00;''"""
Dim strCodeInsert As String
Dim rgeCell As Range

Dim i As Long
Dim tblsDocument As Tables

Set tblsDocument = ActiveDocument.Tables

'Here we take for granted that the summary table has more than 3 rows
For i = tblsDocument.Count To 1 Step -1
If tblsDocument(i).Rows.Count < 4 Then
tblsDocument(i).Delete
End If
Next

'If only one table left, it is the Summary table, no need to continue
If tblsDocument.Count = 1 Then Exit Sub

'Skip summary table...
For i = 1 To tblsDocument.Count - 1
ActiveDocument.Bookmarks.Add strBookRoot & CStr(i),
tblsDocument(i).Range
'Building string to add all cells in the fourth column of all tables
strCodeInsert = strCodeInsert & strBookRoot & CStr(i) & " D:D, "
Next

'remove extra ", " from string
strCodeInsert = Left(strCodeInsert, Len(strCodeInsert) - 2)

'Set range where to insert field code
Set rgeCell = tblsDocument(i).Cell(4, 4).Range
rgeCell.Collapse wdCollapseStart

'Insert field code in forth cell of fourth row of last table in document
'Presumed to be the summary table
ActiveDocument.Fields.Add rgeCell, wdFieldEmpty, _
strCode1 & strCodeInsert & strCode2, False

End Sub
'_______________________________________
 
T

Tony Jollans

For Each t In ActiveDocument.Tables
For Each r In t.Rows
If Replace(Replace(r.Range.Text, Chr(7), ""), vbCr, "") = "" And Not
r.HeadingFormat Then r.Delete
Next
Next
 
D

Doctorjones_md

Tony,

Thanks for the code to delete the blank rows -- if I may as another
question -- Is there an easy way to (Now) delete any tables having Header
Rows but NO blank rows.

The code you provided me effectively deleted all blank rows from the
tables -- the problem I'm left with now is -- I have a couple of tables with
no rows of data, but have Header Rows with Column Names.
 
T

Tony Jollans

Untested, but what about ...

For Each t In ActiveDocument.Tables
If t.Rows.Last.HeadingFormat Then t.Delete
Next
 
J

Jean-Guy Marcil

Tony Jollans was telling us:
Tony Jollans nous racontait que :
Untested, but what about ...

For Each t In ActiveDocument.Tables
If t.Rows.Last.HeadingFormat Then t.Delete
Next

Or, in case the first row is not defined has a heading row repeat...

Dim t As Table

For Each t In ActiveDocument.Tables
If t.Rows.Count = 1 Then t.Delete
Next

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tony Jollans

Indeed. Good thinking.

--
Enjoy,
Tony

Jean-Guy Marcil said:
Tony Jollans was telling us:
Tony Jollans nous racontait que :


Or, in case the first row is not defined has a heading row repeat...

Dim t As Table

For Each t In ActiveDocument.Tables
If t.Rows.Count = 1 Then t.Delete
Next

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Peter Jamieson

If I can hitch a ride on this thread...

A problem some people encounter when mailmerging to an output document is
that you can end up with loads of tables separated by a single paragraph
mark. Trouble is that you don't appear to be able to eliminate these empty
paragraphs using either Find/Replace, or trying to delete them in VBA. I've
always assumed that that's because Word essentially sees eliminating a
paragraph between two tables as a "join two tables together" operation.

But am I wrong that there is no really simple way to eliminate such
paragraphs? Is there really simple way to join the tables? (I guess I could
work /something/ out but perhaps it's a well-known problem).

The reason that this kind of situation can occur during a mailmerge is that
in order to do certain types of merge, you have to consider including a
table row inside an IF field. But because tables and non-table text mix like
oil and water, you just can't do that without ending up with table row,
paragraph, table row, etc.

Peter Jamieson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top