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

First, let's keep two things separate.

1. The approach Graham originally suggested uses Mailmerge and Mailmerge
fields. When your document is connected to a mailmerge data source and
you re-open it (and even sometimes when it isn't, in some versions of
Word), you get that SQL message. One way to get rid of it is to apply
the registry change described in the following KB article:

http://support.microsoft.com/kb/825765

It sounds like you are currently using that approach. To see what is
causing the additional problems, you should probably try opening the
data source that Graham's macro produces - AFAICS it should be a .doc or
..docx Word document containing a table with one row containing field
names, and a second row containing your data. It might or might not
become clear what is wrong, but for example there shouldn't be any empty
cells.

2. The approach I suggested and which Graham has added to does not rely
on mailmerge, uses DOCVARIABLE fields rather than mailmerge fields, and
does not result in the SQL message. If you haven't tried it, I suggest
you do so now. However, if you have already connected your document to a
mailmerge data source and saved it, you will still get that SQL message
when you re-open it. If possible, start afresh with a new document.




Peter Jamieson

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

Bernie wrote:
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