mail merge macro z

D

davjoh

Sub AutoOpen()
'
' Flatten Macro

Set myMerge = ActiveDocument.MailMerge
If myMerge.State = wdMainAndDataSource Then

ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False
ActiveDocument.Fields.Update
ActiveDocument.Fields.Unlink

For Each aField In ActiveDocument.Fields
aField.Delete
Next aField

ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument

ActiveDocument.Save

End If

End Sub

The macro is contained in a mail merge Word document. The purpose of
the macro is to 'flatten' the document after the mail merge is
complete so that the merge fields become simple text. The macro we
are currently using accomplishes this but we need some more
functionality. The Word docs that contain this macro are what we call
templates. Through our Filemaker Pro database, a template is
downloaded from the server, renamed 'Template.doc' and placed in the
'My Data Sources' folder. This folder also contains the datasource
which is a plain text file called 'casedata.txt'. After the template
is opened and the mail merge fields are populated and the macro has
'flattened' the document, the database sends it back to the server as
a plain document for use.

One of the main problems we have is that on the client side, we have
an array of operating systems (including Mac OsX) and multiple
versions of MS Word, including Word 2007. When a template is first
being drafted Word embeds the path to the datasource in the template
(which is always the same). When the template is used with a
different version of Word however, we frequently get the error message
that 'Word Cannot Find its Datasource". Then an 'Open File' dialogue
box appears pointing right at the casedata.txt file. Even more
frustrating is the fact that two different templates containing the
identical macro behave differently in that some will generate the
error message and others won't.

Secondly, the people who are required to draft and create these
templates in the first instance are not very computer savvy. The
author must disable macros in Word, draft the document and populate it
with merge and re-enable macros before uploading the template to the
server for use. Invariably, the process of enabling and disabling
macros gets mixed up - the macro ends up flattening the document and
the user has to start all over.

To solve these two problems we would like the macro to have some
additional functionality. First, we would like the macro to seek out
and open or connect to its datasource programmatically as opposed to
relying on the embedded path in Word. The datasource (casedata.txt)
and the Word template are always in the same folder (My Data Sources
in Windows and Documents in Mac). The macro would never have to look
beyond its current folder.

Second, we would like the macro to query the name of its host Word
file and if it is called 'template.doc', to continue with the macro
and flatten the document and then end. If the name of the host Word
document is anything other than 'template.doc' we would like the macro
to NOT flatten the document and then end.
 
D

Doug Robbins - Word MVP on news.microsoft.com

Why don't you just execute the merge to a new document. There will be no
merge fields in that document, just the data from the data source and it
will not be a mail merge main document of any type.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
P

Peter Jamieson

Some questions/hints rather than a complete solution:

1. I have to assume you aren't using Word 2008 (no VBA, of course, and
you would have to approach this using another automation language)

2. In essence, if your document is connected to a data source when it is
opened, and the data source does not exist, AFAIK there is no way around
that dialog box. You simply can't suppress it. So when the mail merge
main documents are prepared, you have to detach the data source from the
mail merge main document.

3. It's not completely clear what your process is, here, because as far
as I can see you are flattening /the mail merge main document/ rather
than its output. What I am more familiar with is a macro in the mail
merge main document that does

- set docObject = Activedocument, i.e. the mail merge main document,
because if you merge to a new document, Activedocument will change
- set up the merge (e.g. specify type, destination, connect to data source)
- merge to a new document, which then becomes the ActiveDocument
- do what you want with that new ActiveDocument (e.g. flatten fields)
- do what you want with docObject

4. One consequence of that is that I'm not sure what you mean by the
"host file" - if it is simply "the document that is active when the
macro is run" then you can compare Activedocument.Name or
ActiveDocument.Fullname with the hard-coded fiel/pathname you want to check.

4. To get the full pathname of the data source, I think you can use
something like

strDataSourcePath

docObject.Path & Application.PathSeparator & "casedata.txt"

However, you will need to verify that.

5. You should then be able to open the data source using

docObject.OpenDataSource _
Name:=strDataSourcePath

(however, this relies on Word not deciding to ask you for field/record
delimiters)

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