View Single Post
  #14   Report Post  
Posted to microsoft.public.word.docmanagement
Bernie Bernie is offline
external usenet poster
 
Posts: 16
Default How to Merge data from an ascii file into a Word Doc

Hi Everyone,
I really appreciate all this advice.
I have it working -- sort of --
I get this message box:
"Opening this document will run the following SQL command sel * from
D:\....\Merge.doc data from your database will be placed in the document, do
you want to continue"
Of course I want to continue, but is there a way to eliminate this nuisance
message?

Furthermore when continuing I get:
Record 1 contained too few data fields then I get another message
record 1 contained too few field delimiters (,) .
Then another box comes up titled "Header Record Delimiters" it shows , &
(Enter)
I just click OK and my document appears with all the fields filled in just
the way i want it.
So what can I do to eliminate these messages?

"Graham Mayor" wrote:

I tested with an ascii file (created from the PrintFolders utility that can
be downloaded from my web site) which does not produce paragraph endings of
the type used in Word, but which are CHR(13) which only look like paragraph
endings and thus when imported into Word does not make paragraphs that vba
can act upon.. This was the reason for my rather blunt approach in the first
macro. Reading the ascii file directly negated that approach. I haven't yet
tested it with line feed characters (CHR(11).

I think we need to see if this approach will work for the OP before putting
any more effort into fine tuning the macros.

--

Graham Mayor - Word MVP

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



Peter Jamieson wrote:
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