Saving tables in text format

V

Vince

I have a table in Word that I want to save in text format. Normally, when I
select the table and Copy (to clipboard) and paste it in (say) Notepad, the
table looks like this:

British authors Asia Europe Anglo-Saxon (exp. UK) Rest of world Total
1970s BJIR 79 13 2 94
84.0% 13.8% 2.1% 100%
IRJ 60 5 1 66
90.9% 7.6% 1.5% 100%
Total 139 18 3 160
86.9% 11.3% 1.9% 100%
1990s BJIR 142 5 10 36 2 195
72.8% 2.6% 5.1% 18.5% 1.0% 100%
IRJ 151 4 12 21 7 195
77.4% 2.1% 6.2% 10.8% 3.6% 100%
Total 293 9 22 57 9 390
75.1% 2.3% 5.6% 14.6% 2.3% 100%

which is what I want. However, when I tried to do this programatically with
a code like:
"
ST=selection.txt
FF = FreeFile

Open InputFile For Output As #FF

Print #FF, ST

Close (FF)
"

The file that is written ends up looking like this:


British authors
Asia
Europe
Anglo-Saxon (exp. UK)
Rest of world
Total

1970s
BJIR
79


13
2
94



84.0%


13.8%
2.1%
100%


IRJ
60


5
1
66



90.9%


7.6%
1.5%
100%


Total
139


18
3
160



86.9%


11.3%
1.9%
100%


1990s
BJIR
142
5
10
36
2
195



72.8%
2.6%
5.1%
18.5%
1.0%
100%


IRJ
151
4
12
21
7
195



77.4%
2.1%
6.2%
10.8%
3.6%
100%


Total
293
9
22
57
9
390



75.1%
2.3%
5.6%
14.6%
2.3%
100%


Could somebody please tell me how I could copy a table from word in ascii
format so that it would look the same way as it does when I do the process
manually (by copying and pasting). I tried replacing chr(11), chr(10) by
chr(9) but they also produce equally disastrous results.

I would appreciate any help on this.

Thanks a lot,

Vince
 
C

Cindy M -WordMVP-

Hi Vince,
Could somebody please tell me how I could copy a table from word in ascii
format so that it would look the same way as it does when I do the process
manually (by copying and pasting).
Take a look at the ConvertTableToText method in the VBA Help.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
H

Helmut Weber

Hi Vince,

depends all on how much regularity there is in your tables.
Are there merged cells? Are there cells containing more than
one line?
Method A: Use a new document, set it's range to the table
in the original document, convert the table to text, save it as text.

Method B (unsafe): Copy the table, start notepad, sendkey ctrl v.

Method C: Iterate through rows and cells,
get the cell content, put it in a string, add a tab,
add a vbcr at the end of a row until all of the table is
in the string, write the string to a file.


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
V

Vince

Thank you Helmut and everybody else.

Actually, I wrote a Perl program that changes the table into XML. If there are
merged cells, my VBA code will split the cells first (it works on most
tables).

Somehow, when I tried Covert Table to Text, the output is not the same as it
was when I copied the table in notepad.

I will adopt method (c) or method (b). I think method (c) will work for sure.

Thanks a lot.
Vince
 
H

Helmut Weber

Hi Vince,

you may even split the table into an array, like this:

Dim arr() As String
arr = Split(ActiveDocument.Tables(1).Range.Text, Chr(13) & Chr(7))
MsgBox UBound(arr)

That would be plus end-of-row mark, which needs special treatment.

Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
V

Vince

Helmut and everybody else,

It worked after I convertedtabletotext (vbtab) and changed CHR(13) to Chr(10).
The effect of selection.text was then the same as copying and pasting the
table in notepad.

Thanks a lot for all your help.

Vince
 
Top