Convert Table to Text but customize output

K

Kevin

All,

I thought what I was wanting to do would be really simple but it turns
out it's not so straightforward.

I have a table, let's say like this:

---------------------
| Col 1 | Col 2 |
---------------------
| A | B |
---------------------
| C | D |
---------------------

I want to convert this table to text, but using Word's built-in
feature will not do be any good. I need the format of the text to be:

Col 1: A <linebreak>
Col 2: B <paragraph>
Col 1: C <linebreak>
Col 2: D <paragraph>

I thought the fastest way to do this would be to convert the table to
text using linebreaks as the separator and that does work for most
tables...the problem is when a table has a linebreak or paragraph in
it already! Then it gets harder to put Col 1 and Col 2 in the places
where they belong.

Does anyone have any tips on how to approach this problem? Is my
approach solid?

Thanks!!
 
D

Doug Robbins - Word MVP

Use the following code

Dim i As Long, j As Long
Dim crange As Range, Target As Range
With Selection.Tables(1)
Set Target = .Range
Target.Collapse wdCollapseEnd
For i = 2 To .rows.Count
For j = 1 To .Columns.Count
Set crange = .Cell(i, j).Range
crange.End = crange.End - 1
Target.InsertAfter "Col " & j & ":" & vbTab &
Replace(crange.Text, vbCr, vbCr & vbTab) & vbCr
Next j
Next i
.Delete
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, originally posted via msnews.microsoft.com
 
K

Kevin

Doug,

This is exactly what I needed.So much simpler and elegant than my
approach. Thank you very much..

Kevin
 

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