How to get data from txt file into table of word template through

S

Sam

HI,

If I want to load data info from pure txt file into specific cell of one
table
in Word document, what kind of method should I use? Could you give me
some simple code sample?

For example, I have a 'txt' file, with some contact info(Incluing
name,telephone number, address,Email...)

I want to use those contact info to fill one table(which have two columns,
name and email) in word document. In the table, it only give the name info,
we should fill the email column by ourself. How can we collect info from the
'txt' file? it is possible to use VBA to fill the telephone info
automatively?
Could you give me some hints?

Regards

Sam
 
J

Jezebel

Open the text file, extract the data, insert it into the document.

There are well-documented methods for each of these tasks. What have you
tried so far, and what are you having trouble with?
 
S

sam

Hi,

Thank you for answer!

As the data info in the txt.file is not in standard table format, How
can I extract the exactly data info from the txt.file.

The data format in txt file is something like this:

Sam, (27, US, +1-513-1389, (e-mail address removed))
Lisa: (UK, 28, +44-318900, (e-mail address removed))

the table in word template has two column, and it gives out the name
info:
Name Telephone
Sam
Lisa
....

It is possible to extract info from TXT.file and insert into table in
word using VBA. Could you give me some coding info for this sample?

Best Regards

Sam
 
J

Jezebel

It's like asking "How do I drive from Paris to Moscow, where do I turn
left?"

There's a whole heap of commands for parsing strings. You could use Split()
to divide the lines up by the commas. Or maybe just dump the lot into Excel
and use the Text-to-columns function.

As before: what have you tried so far, and what went wrong?
 
S

Sam

HI, Jezebel:

Actually,I am new user to VBA programming. Now i am reading literature in
this field. I am thinking about the best way to load data into table of Word.
I haven't done any test in pratical VBA progarmming. IF the data source file
can be in PDF, DOC, TXT or XML format, which one is the best one to parse
using VBA? Do you have some experience on this issue? And could you recommend
some relevant material to me?

Thanks In advance!

Regards

Sam
 
J

Jezebel

This is a big topic, and it's not at all obvious what you're asking. By
definition, XML is easiest to parse because it's structured specifically to
be parsable; but if it's XML why bother with VBA? -- why not let Word or
Excel parse it for you? TXT and DOC files are parsable to the extent that
they're structured (they are, at least, easy to read). You can't do anything
with a PDF without converting it to something else first (which isn't always
possible).

You seem to be asking two questions: 1) how to parse a source file, and 2)
how to use VBA to populate a Word document. These are entirely separate
issues. Stay with the literature.
 
S

Sam

HI, Jezebel

Absolutely, I think you have got my problems. I am a little bit confused on
these two issues. A lot of new ideas came out during these days. first i want
to parse data souce and then populate the specifc data into Word? Is VBA the
best solution? or something else is better?

For the XML data source file, you said Word can parse it by itself. you mean
field in word can parse XML file or other applications? Could you elaborate
it a bit more for me?

Thank you again !!!!!!

Sam
 
J

Jezebel

Word and XML: this is well covered in Word's own documentation. Word can
open an XML file, and show a pane to display the tags etc. Excel can also
work directly with XML files.

There's no way to answer the question about whether VBA is the best solution
without knowing the problem. It's obviously a good method for working with
Word documents; but given that its days are numbered, you might be better
with VB.Net; or piss off Microsoft entirely and work with Java.
 
S

Sam

HI,Jezebel

I assumed one problem like this:
I have a XML file including all customer info in detail which is regards as
data source.
<?xml version="1.0"?>
<Catalog>
<Customer>
<Name>Bob</Name>
<COUNTRY>USA</COUNTRY>
<COMPANY>IBM</COMPANY>
<Email>[email protected]</Email>
</Customer>
<Customer>
<Name>Sam</Name>
<COUNTRY>USA</COUNTRY>
<COMPANY>Apple</COMPANY>
<Email>[email protected]</Email>
</Customer>
.......
</Catalog>

On the other hand, I have a word document which has a lot of tables inside.
here it is one example of the table in word document,
name | Company | Email
Nina
Laura

There are also other tables with some more or less columns. the common thing
of all the tables is: only the specific info of name column is given out.
Normally, I need first find info in XML file(all the info can be found in XML
file) and fill the table manually. It wastes time.

I think it is possible to populate the specific info from XML file into
Specific cell in the table using some programming. In this manner, it can
generate the final document with all required info automatically.

What is your idea? Could you give me some suggestions?Which programming
lanuage is the best one to solve this problem. Thank u!

Best Regards

Sam
 
J

Jezebel

It's like asking which is the best car for driving to Nebraska. Why do you
need to populate the tables in the Word document at all? Why not write an
XLST file to transform the XML file into the output you need?
 
S

Sam

Hi,

First, not all customer info need to be in the word document. The word
template can tell me what kind of customer and correspondent info it needed.
Secondly, the word template also regulate the layout of text and table. So I
think we need to populate cells in the tables based on word document.

Regards

Sam
 
J

Jezebel

Why do you need a Word document, and what does it give you that can't be
derived directly from the XML?
 
S

Sam

Yes,I see. Based on some requirement or condition, we can derive some info
from XML directly. From a scientific perspective, I think what you have
mentioned is related to the issue about sematic web? Am i right? It is also
popular topic nowsdays.
However,for this time, I Just assume that I need to populate the info into
word document.:) Did you have some relevant experience on the issue? If so,
could you give me some suggestion?

Regards

Sam
 
J

Jezebel

It's called a transformation, and it's nothing to do with semantic webs or
scientific perspectives. It's part of the functionality of Word.

Apart from which, itseems to me that we're going around in circles. Which I
guess makes sense, since you seem determined to reinvent the wheel. But
specifically: YES, VBA is well able to pull data from a text file and
populate a table in a Word document.
 
S

Sam

hi,Jezebel

Anyway, Thank you for your help!

If I have further question, I will come back to you!:)

Regards

Sam
 
A

Akash

Dear Sam,
I understand that Jezbel took you in circles and ultimately left you with
nothing. While these are value judgements whether xml or word is better.
Whatever you want to do can be done using Perl with the help of
Win32::OLE module. Getting a word object would require a code like this:
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3; # die on errors...
my $Word = Win32::OLE->GetActiveObject('Word.Application')
|| Win32::OLE->new('Word.Application', 'Quit'); # get already active Word

Then you can use OLE of windows by using the object $Word. I will suggest
you opne the Tools->Macros->Visual Basic Editor and then use Object Browser
to get OLE functions and those can be used straightway.
Best of luck.
Akash
 
S

Sam

HI, Akash

Thank you for your suggestion!I think it is a good idea.

Do you have some relevant experience about populating the some data from XML
file into specific cell of table in Word document? Could you give me some
hints? I
have posted one sample in the ninth message of this question thread, you can
check it. Could you give me some advices? In my opinion, I think VBA can
solve this problem.

Thanks in advance!

Regards

Sam
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?U2Ft?=,
Do you have some relevant experience about populating the some data from XML
file into specific cell of table in Word document?
You use whichever XML parser (Perl, MSXML, whatever) to get the value from the
XML file. To put it into a Word table cell, you first have to pick up the
table, then specify the cell. Assuming the table in question is the first in
the document, and the target is the first cell in the second row, that would
be, in VBA-speak:
Set tbl = ActiveDocument.Tables(1)
tbl.Cell(2,1).Range.Text = sXMLdataString

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 :)
 

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