View Single Post
  #5   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

My second paragraph explains what a merge data format looks like - the
simplest format is a table with a header row, but it could be a comma
delimited file with a header row. My last paragraph has links to tutorials
for the mail merge process.

As we don't know what Word version you have and as this is an ASCII file the
following macro contains more code than strictly necessary, but it should
create a data file in Word Doc or Docx format from your list without
altering the ASCII file. You can then use that data file in connection with
a mail merge. The fields are named Field_1 to Field_n where n is the number
of rows in your list and reflect the order in which they appear in the list.
Insert the fields in your document where you want the data to appear.

Sub CreateDataFormat()
Dim lNum As Long
Dim i As Long
Dim fNum As Long
lNum = 0
fNum = 0
With Selection
.HomeKey wdStory
.Find.Execute findText:="^13^13", _
ReplaceWith:="", _
Forward:=True, _
Replace:=wdReplaceAll
For i = 1 To ActiveDocument.Characters.Count
If ActiveDocument.Characters(i) = Chr(13) Then
lNum = lNum + 1
End If
Next i
.Find.Execute findText:="^13", _
ReplaceWith:=", ", _
Forward:=True, _
Replace:=wdReplaceAll
.EndKey wdStory
.Find.Execute findText:=", ", _
ReplaceWith:="", _
Forward:=False, _
Replace:=wdReplaceOne
.HomeKey wdStory
For i = 1 To lNum
fNum = fNum + 1
.TypeText "Field_" & fNum
If i lNum Then .TypeText ", "
Next i
.TypeParagraph
End With
ActiveDocument.Select
WordBasic.TextToTable ConvertFrom:=2, NumColumns:=lNum, NumRows:=2, _
InitialColWidth:=wdAutoPosition, Format:=0, Apply:=1184, AutoFit:=0,
_
SetDefault:=0, Word8:=0, Style:="Table Grid"
strDocName = Split(ActiveDocument.FullName, ".")
If Application.version = 12 Then
ActiveDocument.SaveAs _
FileName:=strDocName(0) & ".docx", _
FileFormat:=16
Else
ActiveDocument.SaveAs _
FileName:=strDocName(0) & ".doc", _
FileFormat:=0
End If
End Sub

http://www.gmayor.com/installing_macro.htm


--

Graham Mayor - Word MVP

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



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?