Inserting data into text files

S

Smoolander

At work we receive a txt file with client data (comma delimited) directly
from a system. This file then needs to be used as a mail merge data source.

I am trying to write code to open this data source by first asking the user
to specify the file name (input box), and then open the file and insert
headings into the first row of the file (before client data), as these
headings are used as the mail merge headings.

Currently I am stuck on the code necessary to open the txt file and then to
insert the headings (as a string) into the file in the desired location (line
1) without deleting any of the data in it.

So basically process is
File 30060418.txt is received
User opens word template
Runs macro
Asks for file name
Headings inserted into file
Mail merge reads txt file with headings

Any help on the code would be appreciated.
 
D

David Sisson

Currently I am stuck on the code ... without deleting any of the data in it.

You can't insert data into a sequential text file. So the approach is
to create a new text file starting with your header info. Then open
the client data, read a line from it, and then write it to the header
file until you've reached the end of the file.

So off the cuff...
Dim First, Last, Add1, Add2, City, State, Zip as String
'The heading must match the number of elements in client data.

Open "New Client Data" as output as #1
write #1, "First", "Last", "Add1", "Add2", "City", "State", "Zip"

Open "Client Data" for Input as #2
Do
Read #2, First, Last, Add1, Add2, City, State, Zip
Write #1, First, Last, Add1, Add2, City, State, Zip
Loop until EOF(2)

Close #2: Close #1

'Continue with merge using New Client Data


Not tested, but this should get you started in the right direction.

Another advantage of this method is your original data is not modified.
 
S

Smoolander

Thank you David. I thought this may have been the case but just wanted
confirmation.
 

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