Home |
Search |
Today's Posts |
#1
|
|||
|
|||
Merge from a .txt file to multiple Word documents
I'm hoping someone can point me in the right direction. We're currently
using Word 2002 but will be upgrading to Word 2003 this summer. Currently, the users are accomplishing this task in WordPerfect 7, but that will be eliminated shortly. The task is as follows: a txt file is created which is then merged with a variety of different letters to be sent to the public. The text file has the following set-up: first line is a number which identifies a specific letter (1, 2, 3, 4 etc.) second line is a name third line is a street address fourth line is a city, state and zip code What I'm trying to do is to create a merge which will open the appropriate letter (letter 1, letter 2, etc.), fill in the information and then put all these letters in one new document. I know a little Visual Basic, but not enough yet to accomplish this. Can someone point me in the right direction? |
#2
|
|||
|
|||
Although you might just about be able to perform the merge you want with no
VBA at all, there are three main areas in which you are likely to have problems 1. the attempt to process multiple letter types in a single merge, or indeed in a single output document, is probably worth it in Word, except perhaps in the case where all the letters have the same layout and there are no headers/footers 2. using a single-column data source without a header causes a number of problems (e.g., Word 2003 will always prompt for delimiter details if you only have a single column, and record selection/filtering is harder 3. combining the two requires you to use INCLUDETEXT, and because you cannot put { NEXT } fields inside other fields, you have to resort to using SET fields. In my view, you should at least transform the data before attempting a merge, and you should probably do a separate merge for each letter type. As a very simple example of one way that you might transform your data, here's a bit of VBA. I'm assuming that all your data is in a single file, not one file per address, that there are exactly 4 lines per record, and no header line. Sub ReformatFile() ' a very simple sample! ' this sample sub assumes we have a text file called mydata.txt ' and we want to turn each group of four lines into ' one 4-column tab-delimited record, all headed up ' by a specific header record. There are lots of assumptions, e.g. ' that input line recognises the line end used in the file, ' that there are no tabs (so we do not need to quote the ' data), there is an exact multiple of 4 lines (so we do ' not run out of data), the strings are not too long to be ' processed and so on. ' I use the old BASIC file I/O stuff rather than the newer ' filestram objects. No particular reason. Dim sLine1 As String Dim sLine2 As String Dim sLine3 As String Dim sLine4 As String On Error GoTo problem ' You really need to make sure that nothing has these files open ' at this point - e.g. if this code is in your mail merge main ' document, you should disconnect the data source Open "c:\w\singlecolumndata\mydata.txt" For Input As #1 Open "c:\w\singlecolumndata\mydata4.txt" For Output As #2 Print #2, "LetterNumber" & _ vbTab & "Name" & _ vbTab & "Street" & _ vbTab & "CityStateZIP" While Not EOF(1) Line Input #1, sLine1 Line Input #1, sLine2 Line Input #1, sLine3 Line Input #1, sLine4 Print #2, sLine1 & _ vbTab & sLine2 & _ vbTab & sLine3 & _ vbTab & sLine4 Wend Close #2 Close #1 Exit Sub problem: MsgBox "Error number " & _ Err.Number & _ ", " & Err.Description & _ " occurred when transforming the data source" Err.Clear On Error Resume Next Close #2 Close #1 End Sub If you have one mail merge letter per letter number, you can filter the data using the standard Word mailmerge features. To perform the merge with no VBA, you could try the following, but I think you will probably be wasting your time. I'm making the same assumptions about the source data as above, and assuming that your letters are similar with no headers/footer. In that case, you could proceed as follows: a. if the data file does not have a column header (one line with a column name in it, say "mydata", then you would either have to add that row, or create a separate plain text HeaderSource file containing mydata b. If you need a HeaderSource file, you will need to define a Header source in your mail merge main document. In Word 2002/2003 you either have to do that by dragging the old MailMergeHelper out of Tools|Customize|Category All Commands and using the second button down to define the data source, or using VBA. c. Connect to the data source in the usual way. d. You will need one .doc for each letter, named so that you can construct the pathname of the .doc from the letter number in the data. Let's suppose you have letters in c:\myletters called myletter1.doc, myletter2.doc etc. e. put the following fields in your main document. All the {} need to be the field code braces you can insert using ctrl-F9 { SET LetterNumber { MERGEFIELD mydata } }{ NEXT }{ SET Name { MERGEFIELD mydata } }{ NEXT }{ SET Street { MERGEFIELD mydata } }{ NEXT }{ SET CityStateZIP { MERGEFIELD mydata } }{ INCLUDETEXT "c:\\myletters\\myletter{ LetterNumber }.doc" } f. In each letter, use the field codes { Name } { Street } { CityStateZIP } to insert the relevant texts g. merge to an output document h. immediately select the whole output document (e.g. using ctrl-A) and use ctrl-shift-F9 to "unlink" all the field codes. This solution can be extended in a number of ways, e.g. you might be able to get different headers/footers by using additional INCLUDETEXT fields in your mail merge main document's header/footer areas. But it really seems very shaky to me. Peter Jamieson "abs" wrote in message ... I'm hoping someone can point me in the right direction. We're currently using Word 2002 but will be upgrading to Word 2003 this summer. Currently, the users are accomplishing this task in WordPerfect 7, but that will be eliminated shortly. The task is as follows: a txt file is created which is then merged with a variety of different letters to be sent to the public. The text file has the following set-up: first line is a number which identifies a specific letter (1, 2, 3, 4 etc.) second line is a name third line is a street address fourth line is a city, state and zip code What I'm trying to do is to create a merge which will open the appropriate letter (letter 1, letter 2, etc.), fill in the information and then put all these letters in one new document. I know a little Visual Basic, but not enough yet to accomplish this. Can someone point me in the right direction? |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
is word perfect compatible with office word? | Microsoft Word Help | |||
How do I keep two words together? | Microsoft Word Help | |||
Locking Two Words Together to Make a Proper Compound Noun in Word | Microsoft Word Help | |||
What is the difference between Word Perfect and Microsoft Word, a. | New Users | |||
letters - ask/fillin | New Users |