View Single Post
  #11   Report Post  
Posted to microsoft.public.word.docmanagement
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default How to Merge data from an ascii file into a Word Doc

Jolly good, Graham.

One thing to be careful of when dealing with Doc variables is what
happens to stuff like LF characters etc. when you insert them using
DOCVARIABLE fields. I can't remember off the top of my head what the
problem is (there's a difference between Word 2007/2003 or some such
too) but didn't think it was likely to be a problem for the current
prooblem. For a more general solution you might have to modify the
strings before they go in the Variables.

Peter Jamieson

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

Graham Mayor wrote:
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