concatenate two table cells- get rid of extra hidden chars?

K

KR

I am cycling through my documents concatenating the contents of two cells
(cell.range) in each row of source tables. I do so by taking
left(Cell1.text, len(Cell1.text)-2) & Cell2.text

This does put both cell contents together, but it leaves me with an extra
hard return between cell1 and cell2 (cell2 text starts on a new line in the
destination cell). I then copy/paste these cells to Excel, which causes a
problem because Excel automatically breaks this into two rows, whereas I
need it all in one.

Can someone point me to a resource that explains what hidden codes exist
within each table cell, and whether the commands Left (and perhaps Right)
are sufficient if I trim off the appropriate number of characters, or if I
need to do something else to ensure that the code will be robust? I don't
create the source documents, so I am worried about any variability that
might be caused by what the users send me.

Thanks!
Keith
 
K

KR

I've now taken a number of samples and pasted into Excel; I'm also seeing
evidence of additional hard returns at the end of Cell2, e.g. in Excel there
are sometimes (not always) blank rows after the Cell2 text from my Word
table. I'm interpreting this to mean that there are possibly several hidden
characters in each Word table cell, and whether or not they are really there
may vary from cell to cell.

Ultimately, my question is:
"how do I pull the contents from multiple cells and grab /only/ the text and
not hidden characters, so that when I concatenate and paste it into a new
cell, it doesn't have any extra characters"

I'm using Word2003, although I'd like to remain compatible with older
versions (back to Word97 if at all possible)

Thank you,
Keith
 
D

Dave Lett

Hi Keith,

You can probably use something like the following as an example:

Dim oRngC1 As Range
Dim oRngC2 As Range
Dim oTbl As Table
Dim iRw As Integer

Set oTbl = ActiveDocument.Tables(1)
For iRw = 1 To oTbl.Rows.Count
Set oRngC1 = oTbl.Rows(iRw).Cells(1).Range
Set oRngC2 = oTbl.Rows(iRw).Cells(2).Range
oRngC1.MoveEnd unit:=wdCharacter, Count:=-1
oRngC2.MoveEnd unit:=wdCharacter, Count:=-1

ActiveDocument.Paragraphs.Last.Range.Text = oRngC1.Text & vbTab &
oRngC2.Text
Next iRw

If you have carriage returns (paragraphs), then you will want to use the
Replace statement, as in the following example:

ActiveDocument.Paragraphs.Last.Range.Text = Replace(oRngC1.Text,
Chr(13), "_") _
& vbTab & Replace(oRngC2.Text, Chr(13), "_") & vbCrLf

HTH,
Dave
 
D

Doug Robbins - Word MVP

I would use

Dim cell1 as Range, cell2 as Range, Result as String

With ActiveDocument.Tables(n)
Set cell1 = Cell(i, j).Range
cell1.End = cell1.End - 1 'strips off the end of cell marker
Set cell1 = Cell(k, l).Range
cell2.End = cell2.End - 1 'strips off the end of cell marker
Result = cell1 & " " & cell2
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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