View Single Post
  #7   Report Post  
Posted to microsoft.public.word.docmanagement
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default How to Merge data from an ascii file into a Word Doc

Good thinking - That has the makings of a plan

I have taken the liberty of making a couple of minor changes to cover the
issue of one or more empty lines in the text file and to remove all relevant
docvariables in the document that may have been created by a previous longer
file, for I have the feeling that this is going to be a repeat requirement.
I have also added a message box at the end so the user knows how many
variables are present. If the file is always going to be of the same length,
this message box can be omitted.

Sub TransferTextRowsToVariables()
Dim iCount As Integer
Dim strLine As String
Dim vVar As Variant
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
For Each vVar In oVars
If InStr(1, vVar.name, "line") Then
vVar.Delete
End If
Next vVar
' use your file name here
Open "c:\a\ascii.txt" For Input As 1
iCount = 0
While Not EOF(1)
Line Input #1, strLine
iCount = iCount + 1
On Error Resume Next
'ActiveDocument.Variables("line" & iCount).Delete
Err.Clear
On Error GoTo 0
If Len(strLine) 0 Then
ActiveDocument.Variables.Add "line" & iCount, strLine
Else
iCount = iCount - 1
End If
Wend
Close #1
MsgBox "Variables line1 to line" & iCount & " created"
End Sub

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Peter Jamieson wrote:
If you have to resort to using a macro, the following approach might
be simpler:

Run the following macro to transfer each line into a "document
variable" so that variable "line1" contains the first line from your
file, variable "line2" contains the second line, and so on.

Then you can insert (say) the 3rd line using a field like

{ DOCVARIABLE line3 }

Sub TransferTextRowsToVariables()
Dim iCount As Integer
Dim strLine As String
' use your file name here
Open "c:\a\ascii.txt" For Input As 1
iCount = 0
While Not EOF(1)
Line Input #1, strLine
iCount = iCount + 1
On Error Resume Next
ActiveDocument.Variables("line" & iCount).Delete
Err.Clear
On Error GoTo 0
ActiveDocument.Variables.Add "line" & iCount, strLine
Wend
Close #1
End Sub

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Bernie wrote:
Hi Graham,
Your first paragraph is correct. Can you tell me what a "merge data
format" looks like?
Then I could add that to my ascii file. I need to automate this
process so going thru Excel is not a choice.
Also, can you give me the steps for specifying how to create the
merge field into the Word doc and how do I specify to Word, to get
the data from my ascii file.
Thanks in advance for your help.

Bernie

"Graham Mayor" wrote:

I assume that the list will be merged into various places in the
same document, rather than one place in 20 different documents?
That being the case, you will need first to convert the list to a
merge data format.

This is probably simplest to do in Excel. Open the text file in
Excel then transpose the list so that the entries are all in one
row. insert a header row and name the columns A, B, C etc will work.

Save the file as an Excel document and use that as a merge data
source for a letter merge. Insert the 20 fields where you want
them. Merge to a new document.

If the list is to be merged into the same place in 20 documents,
open the list in Word convert the text to a one column table. Add a
row at the top and give the column a name. Save as a document and
use that as a merge data source.

The principles of mail merge are covered at
http://www.gmayor.com/mail_merge_lab...th_word_xp.htm or
http://www.gmayor.com/merge_labels_with_word_2007.htm
--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Bernie wrote:
I want to merge fields from an ascii file into a word document.
The ascii file has a word or phrase on each line. Each line will
be a merged field. There will be approx 20 lines to be merged into
various places in the document.
How do you set up the word doc to receive these fields?