[AccessD] Mail Merge

Henry Simpson hsimpson88 at hotmail.com
Sat Sep 27 18:06:19 CDT 2003


Sounds like an automation/write to bookmark approach with custom toolbars 
problem to me.  I can't help with the toolbars, but they are analagous to 
Access and Excel toolbars, both of which I have some experience with.

The easiest way to protect a part of the document is to make the template 
with formfields and then protect for formfields.  If you write to a 
formfield bookmark with automation code, the formfield is replaced with the 
text and becomes uneditable together with the rest of the document.  If you 
want to leave editable fields, you write code to create a new formfield and 
set the text.  When you are done, you set the protection with the noreset 
option.  Assuming a Word Document object, oWdDoc with a formfield that has a 
bookmark name "NType" and a reference to a Word Application object, oWdApp:

        oWdDoc.Bookmarks("NType").Select
        oWdDoc.Bookmarks("NType").Delete
        oWdApp.FormFields.Add Range:=.Range, Type:=wdFieldFormTextInput
        oWdDoc.FormFields("text1").Name = "NType"
        oWdDoc.FormFields("NType").Result = "some text value"
        oWdApp.ActiveDocument.Protect Type:=wdAllowOnlyFormFields, 
noreset:=True
        strFileName = 'function that retrieves default temp folder, saving 
and deleting in a fixed name
        oWdDoc.SaveAs strFileName

If you use formfields and don't delete them and create them afresh, then 
they are not editable fields.  This works with all manner of Word 
formfields.  This code leaves them as editable fields, thanks to the noreset 
option, that users can tab between.  I've found it useful to save the file 
in a temp location and overwrite it each time the routine is run (kill if it 
exists first), but you can get rid of the standard menu bar and control 
printing/saving with a replacement menu and skip saving in the Word document 
close event that you can hook.

Word Docs also have a number of properties like Title, Subject, Author... 
that you can set with the query name from which a predefined query can be 
called by automation code.  Alternatively, you can save the query name as 
the lowest bookmark in the collection by prefixing a few low ASCII 
characters that you strip with the mid$() function.  For data, I use a 
bookmark name that is parsed by a routine that sets the appropriate field 
from a recordset to that value.  If you have bookmark named 'DocumentDate', 
the procedure can use the rst("DocumentDate") field as the data source.  
What I've found is that the same data is oft times repeated in a document so 
I allow some dummy characters such as 'DocumentDate_01', DocumentDate_02' 
and parse to the underscore delimiter.  In addition, I also parse out a 
formatting string and a data type separated by underscores so an appropriate 
function can be called to set a format for dates, currency and other custom 
formats.  If the first format letter is 'd' or 'o', a select case passes the 
date format function the data and returns the string and the 'o' tells it I 
need an ordinal value (1st, 2nd, 3rd...)  In this fashion, the name of the 
bookmark determines which field is written to it as well as the format.

Depending on the complexity, I've found that I may need to run a number of 
documents with repeated data.  In that case, the routine that fills the Word 
template is passed a collection of standard data common to all documents and 
it then adds a new collection item for each data element in each record of 
the recordset as each document is created in sequence.  For this, I use a 
procedure that adds the base recordset to the collection, indexed by field 
name, and one that adds subsequent individual record values.  This approach 
has been flexible and reliable for my purposes.

Hen

>From: "John Colby" <jcolby at colbyconsulting.com>
>Reply-To: Access Developers discussion and problem 
>solving<accessd at databaseadvisors.com>
>To: "AccessD" <AccessD at databaseadvisors.com>
>Subject: [AccessD] Mail Merge
>Date: Fri, 26 Sep 2003 21:49:17 -0400
>
>OK, my client's mail merge project is getting off the ground and I have no
>experience at this and I need some info.
>
>Background: (How I envision it)
>
>The system will allow the user to select contact info - name / address /
>phone from up to 3 different tables, placing the selected contact info into
>a local table in the FE.  One address will be designated as the "To"
>address, with all others CC addresses.
>
>This info will be used to build up a merged document with info such as 
>claim
>number, claim info etc.  The "TO" address will be embedded in the document
>along with the other data.  However many addresses are selected will
>determine the number of copies of this document printed.
>
>A second document will be printed with only the address info, this to be
>used to slide into the envelope window to get the "CC" copies to the right
>place.
>
>The user can select the document they wish to send as part of this process,
>i.e. there will be a combo that pulls all the document names out of a
>directory and allows selecting which document to send.
>
>I will create a query for each document, and (preferably) the document 
>knows
>what query to use to pull the data.  The queries will in all cases be
>filtered to a specific claim simply because they are on that claim when 
>they
>click on a tab to do this mail merge process.  IOW, they go to (actually
>live in) the claim form which already has many tabs displaying child info 
>to
>the claim.  I will be adding another tab that allows them to select the
>document to merge, the people to send the doc to and then press a button
>that fires off a process to do it all.
>
>Questions:
>
>* Can I open a mail merge document and cause the merge to happen, but leave
>the doc open to allow edits?
>* Can I lock specific areas of the document so that those areas cannot be
>edited?
>* Is the merged data ever editable?  I may need to lock some but allow 
>other
>merged data to be edited.
>* Assuming I can do that, can I then tell the document (Or Word) to print N
>copies of the document where N may change from day to day.  IOW, I do not
>want the user to have to remember (or even have to know) how many copies to
>print.  So the doc opens, they edit it, then somehow say "print" or 
>"Cancel"
>but I have already told Word how many copies to print if they hit the print
>button.
>* After that document closes I need to know if the user actually printed 
>the
>N copies of the document or canceled out (closed word without printing?) so
>that if they did print, I can then print N-1 copies of the address document
>for the windowed envelopes.  How do I know if the user printed or canceled
>without asking them as they come back to Access?
>* Can I somehow prevent saving the document?  They have no use for the
>document itself after printing and I specifically don't want them saving a
>merged document back over the top of the original "Template".  I suppose I
>could brute force that by setting a Read-Only attribute on the hard disk 
>but
>would prefer to somehow disable the save button in Word.  I guess what I
>really want is to disable the menus and toolbars in Windows and enable a
>custom toolbar of my own or something like that.
>
>Has anyone ever done this stuff specifically, REAL specific help on how to
>do this?  I can handle the Access side of things but don't know anything
>about how to make Word do what I want.
>
>TIA,
>
>John W. Colby
>www.colbyconsulting.com
>
>_______________________________________________
>AccessD mailing list
>AccessD at databaseadvisors.com
>http://databaseadvisors.com/mailman/listinfo/accessd
>Website: http://www.databaseadvisors.com

_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*   
http://join.msn.com/?page=features/junkmail



More information about the AccessD mailing list