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.
'
' 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.