|
I think you may want to do much of this in the database (if you can edit the Access database) or export the key table to Excel and add a few fields. The merged document is actually a single document married to a set of records so edits to parts of the document that are not part of some merge field will be the same for each record. You could create some new calculated fields that would contain the entire paragraph you want inserted for certain records and actually place the paragraph in the database multiple times. With short paragraphs like your letter has, this might be an easier solution that what you started.
When you do the mail merge in Word, you can filter which records get merged based on values in some field (effectively running a query). When the small copy of the database pops up in the wizard, you click one of the down arrows in one of the column headings and select 'Advanced' Suppose you have a column heading named "Organ", then you can only merge to the records where the value in that field is "heart" as one example.
If you're only doing one or a few letters at a time, you might still want to use VBA but you might actually be better off with the VBA located in the Access database and have it create new word documents as required. If you do stick with the VBA route, here are a few answers to your earlier questions.
Typically, if you're moving through the document and editing paragraphs and bookmarks, you want to return to the selection point you were at when you started the routine. Do this with something like: ---------------- Dim rngStartMark As Word.Range Set rngStartMark = Selection.Range ' Run your find and modify routine rngStartMark.Select ----------------------
If you want to run some part of the code only if the word "heart" appears somewhere in the range labeled by the bookmark named "keyrange" you could have something like: ---------------------- If InStr(ActiveDocument.Bookmarks("keyrange").Range.Text, "heart") > 0 Then ' Run some code End If
Edit to add some ideas on using Excel to do the work before a mail merge:
Suppose you export your table from the Access database as an Excel Workbook. The table will appear on Sheet1. Let's say you set up Column K with the header "Organ" and you populate the column with the organ(s) appropriate for each record. Now, you have a block of text that you want inserted in your letter, only for those institutions where "pancreas" is one of the organs listed in that field. Go to Sheet2 of the database and enter the text into cell A1 of that sheet (you can enter up to 32,767 characters in each cell, even though only 1,024 will be displayed in the cell, all will be displayed in the formula bar). Now, go back to Sheet1 and set up a new column in your table with header label "TextA". Move to the cell just below TextA (row2, your first record) and enter the Excel formula: =IF(ISERROR(SEARCH("pancreas",K2,1)),"",Sheet2!$A$1) Drag the formula by the lower right cell corner (the autofill handle) to copy the cell down. If you use this field as part of a mail merge now, the text block will only be inserted in the letters prepared for those institutions where the record had pancrease listed in column K.
|