Tab delimited data source

J

janlabe

I have a mailmerge data source created by a DOS program that I have been
using for 13 years through several version of Word (I'm on Word 2002 now).
For some reason, Word sometimes loses track of the data source and I have
never figured out how to reattach it short of copying in an archived copy of
the merge doc. I go through all the steps of manually attaching the file,
but it always says the records are empty (which they aren't). I have tried
all encoding options. It is a simple file with tabs between fields and 0x0D
between records. The file is fine.

How does one attach a tab delimited .txt file as a merge data source?
 
G

Graham Mayor

Word is normally quite relaxed about data sources for mail merge and a tab
delimited text file should not cause it any grief.

The following macro will re-attach the data source named in it to the
current document. However you could make things simpler for Word by
converting the data file to a Word table - see
http://www.gmayor.com/convert_labels_into_mail_merge.htm and storing the
data source either in the same folder as the merge document or in the 'My
Data Sources' folder. You should also check for orphan lock files which
prevent Word from operating correctly - see
http://www.gmayor.com/what_to_do_when_word_crashes.htm

Sub AttachData()
With ActiveDocument.MailMerge
.MainDocumentType = wdNotAMergeDocument
.MainDocumentType = wdFormLetters
.OpenDataSource name:= _
"D:\My Documents\Test\Tab Delimited Data.txt", _
ConfirmConversions:=False, _
ReadOnly:=False, _
LinkToSource:=True, _
AddToRecentFiles:=False
End With
End Sub
http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Peter Jamieson

If your records are definitely only delimited by 0x0D (CR) and not
0x0D0A (CRLF) then you may find that
a. Word always pops up the dialog that asks you for the field and
record delimiters
b. even when you say <tab> and <enter>, no records are found.
c. Graham's code also forces you to respond to that dialog.

I can't check in Word 2002 right now but in Word 2003 or 2003 I believe
what you actually need is something along the following lines. (Let's
suppose your file is called myfile.txt and is in folder c:\a ) :

1. A file called schema.ini in the same folder as myfile.txt. There may
already be one there. It's a text file that you can edit in Notepad.
What you need is some lines like this:

[myfile.txt]
ColNameHeader=True
Format=TabDelimited
MaxScanRows=25
CharacterSet=OEM

(It's possible that you would need something other than OEM in that last
line).

2. A completely empty "Office Data Connection" (.odc) file. You can
create an empty file in Notepad and rename it. Let's say it's called

c:\a\empty.odc

3. A different bit of Word VBA to set up the data source, e.g.:

Sub AttachData()

Dim strFolder As String
Dim strODCFile As String
Dim strConnection As String
Dim strQuery As String
Dim strTextFile As String

' Use your folder name...
strFolder = "c:\a\"

' Use your .odc name...
strODCFile = strFolder & "empty.odc"

' Use your text file name...
strtextFile = "myfile.txt"

' Build the connection string
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Mode=Read;" & _
"Jet OLEDB:Engine Type=96;DataSource=" & _
strFolder & ";"

' Build the Query string
strQuery = "SELECT * FROM [" & strTextFile & "]"

' Open the data source

With ActiveDocument.MailMerge
.MainDocumentType = wdNotAMergeDocument
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:=strODCFile, _
Connection:=strConnection, _
SQLStatement:=strQuery
End With

End Sub


Peter Jamieson

http://tips.pjmsn.me.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top