View Single Post
  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP on news.microsoft.com Doug Robbins - Word MVP on news.microsoft.com is offline
external usenet poster
 
Posts: 407
Default Create a merged document with "text form field" capability?

If by "The default datasource that Word wants to use is an Access Database",
you mean the file that is created when you Type a New List in the Word Mail
Merge facility, the code that I posted will not work with that type of .mdb
file. It only works with a table that is created in a database that is
created in Microsoft Access. I should also have indicated that it is
necessary in the Visual Basic Editor to set a reference under
ToolsReferences to the Micorosft DAO 3.6 (or 3.5) Object library.

Here is a version of the macro that will work with a data source that is in
an Excel Spreadsheet. Once again, it requires the are reference be set to
the DAO object library:

Sub MailMergewithFormFields()
Dim dSource As String
Dim qryStr As String
Dim mfCode As Range
Dim i As Long, j As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
With ActiveDocument
'Get the details of the datasource
With .MailMerge.DataSource
dSource = .Name
qryStr = .QueryString
End With
'Convert the MERGEFIELDS to DOCVARIABLE fields
For i = 1 To .Fields.Count
If .Fields(i).Type = wdFieldMergeField Then
Set mfCode = .Fields(i).code
mfCode = Replace(mfCode, "MERGEFIELD", "DOCVARIABLE")
End If
Next i
'Convert the Mail Merge Main document to a normal Word document
.MailMerge.MainDocumentType = wdNotAMergeDocument
.Protect wdAllowOnlyFormFields, NoReset
End With
' Open the database
Set db = OpenDatabase(dSource, False, False, "Excel 8.0")
' Retrieve the recordset
Set rs = db.OpenRecordset(qryStr)
With rs
' Move to the first record
.MoveFirst
j = 1
Do While Not .EOF
'Create variables in the document with the names and values of the
fields in each record
For i = 0 To .Fields.Count - 1
If .Fields(i).Value "" Then
ActiveDocument.Variables(.Fields(i).Name).Value =
..Fields(i).Value
End If
Next i
With ActiveDocument
.Fields.Update
.SaveAs "C:\Test\MwithFF" & j
End With
.MoveNext
j = j + 1
Loop
End With
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing

End Sub


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

"QA info" wrote in message
...
Great! It's a relief to get help from someone who understands what I'm
trying
to do.

I will look over this and see if I can get it to make sense. I think I can
figure out how to record a macro. The datasource is currently blank, it
would
be created each day from the orders that we receive. The default
datasource
that Word wants to use is an Access Database, so that's what I've been
trying
to learn, unless there is something else that will make more sense.

"Doug Robbins - Word MVP on news.microsof" wrote:

Here is a macro that can be used to do a mail merge with formfields if
the
datasource is a table in an Access Database. The mail merge main
document
must be set up with the data source attached and the merge fields
inserted
into the document. It can be used with a document that contains any type
of
formfield and creates a separate document for each record in the data
source
with a filename of the format MwithFF# in a folder c:\Test (that must be
created on your system). You can change the MwithFF to something else if
you want and also the folder into which the documents are saved by
modifying
the

..SaveAs "C:\Test\MwithFF" & j

line of code.

If you data source is not in that format, the following macro can be used
to
execute the merge to a new document in which the formfields that were in
the
mailmerge main document, will be reinstated into each of the "letters"
contained in that new document: