How to delete blank line at end of table cell?

A

Al

I have an application which is exporting data to a Word table and there are
multiple blank lines in many cells. I do a find ^p^p replace with ^p to
remove the blank lines but this does not remove the blank line at the
bottom of the cell. The last line in the cell ends with a para mark and
the table cell marker is on the next line. If I manually delete the para
mark, the cell marker moves up and the blank line goes away. Can I do this
with find and replace or can someone help with a VBA to do this?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, notre ami < Al > nous laissait savoir que :
In this message, our friend < Al > informed us that:


|| I have an application which is exporting data to a Word table and there
are
|| multiple blank lines in many cells. I do a find ^p^p replace with ^p to
|| remove the blank lines but this does not remove the blank line at the
|| bottom of the cell. The last line in the cell ends with a para mark and
|| the table cell marker is on the next line. If I manually delete the para
|| mark, the cell marker moves up and the blank line goes away. Can I do
this
|| with find and replace or can someone help with a VBA to do this?

Cell markers are tricky, especially with find and repalce.
I do not know if there is a way to refer to a cell marker in the built-in
Find dialog... But with VBA you can play with the following code to solve
your problem.

'_______________________________________
Dim myCell As Cell
Dim CellText As String
Dim NewText As String

Dim SearchStr As String
SearchStr = Chr(13) & Chr(13) & Chr(7)

With ActiveDocument.Tables(1)
For i = 1 To .Rows.Count
For Each myCell In .Range.Cells
CellText = myCell.Range.Text
If Len(CellText) > 2 Then
If Right(CellText, 3) = SearchStr Then
NewText = Replace(CellText, _
SearchStr, Chr(7))
myCell.Range.Text = NewText
End If
End If
Next myCell
Next i
End With
'_______________________________________

Good luck ;-)

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

Klaus Linke

Hi Al,

You can't search for the end-of-cell markers.

If you copy the text from a cell into a string, the end-of-cell marker shows
up as Chr(13) & Chr(7) ... and the end-of row-marker as two of these.

But neither searching for ^13^7 nor for ^13 or ^7 will find them, so you
can't deal with your problem with Find/Replace.
I wrote to (e-mail address removed) about it in the past -- It would really be
nice if you could find the end-of-cell markers with Find/Replace.
It's very doubtful that this will ever come to pass though, no matter how
many people request it. That's the Curse of Compatibility, which makes Word
worse and worse with each new version.


With VBA, you can loop the cells and delete the trailing paragraph marks; a
macro that does this for all cells in all tables:

Dim tableLoop as Table
Dim cellLoop As Cell
For each tableLoop in ActiveDocument.tables
For Each cellLoop In tableLoop.Range.Cells
While Right(cellLoop.Range.Text, 3) = Chr(13) & Chr(13) & Chr(7)
cellLoop.Range.Characters.Last.Previous.Delete
Wend
Next cellLoop
Next tableLoop

Maybe you might be able to write a faster macro if your tables are very
simple (no merged cells, no formatting except styles, no more than one
paragraph except for the trailing ¶ marks) by copying the text of a table
into a string (strTable=tableLoop.Range.Text), deleting the trailing
paragraph marks with string functions, and copying back the text.
Though if you want to set tableLoop.Range.Text=strTable, it's expected that
cells are delimited by tabs (instead of end-of-cell markers), and rows by
paragraph marks, so it would get a bit messy.

Regards,
Klaus
 

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