Access & Word

I

Ivan Grozeny

Sandra wrote...
-----Original Message-----
Is there a process to merge Access fields into a Word
document???
.

Ms. Sandra

Assuming that you want to use MAIL MERGE approach, it is
possible. Takes a little work on the WORD side. You will
need to put text fields in your word document. To do this
you will need to add the forms tool bar onto your word
document and then insert a 'text form field'. Give it the
properties you want, and the name of the field from access.
I generally leave the properties alone and just fill in
the bookmark name.

The access side takes a bit of VB code to make it work.
Here is a simple solution but you will need to hard code
the path to the word document. There are ways around that
of course but... Anyway, here is an example. The rest of
this code will allow you to merge in the data from ACCESS.
This a simple one we use to create job rejection letters.
Feel free to email me if you have further questions.

It will loop through to get all the records from the query.

Begin Code
*********************************************************

Dim docLetter As Document, appWord As Word.Application
Set appWord = CreateObject(Class:="Word.application")
'open the Word document
Set docLetter = appWord.Documents.Open("u:\HR\REJECT HR
Ltrhead.doc")

Dim con As ADODB.Connection
Dim rst As ADODB.Recordset
Set con = Application.CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open Source:="zDOCQuery", ActiveConnection:=con,
CursorType:=adOpenForwardOnly, LockType:=adLockPessimistic

' Begin the loop
' This starts the query that gets the data using
' the text field names that are in the word doc
Do Until rst.EOF = True
If (Application.Forms("DOCquery").Controls("CODE") =
rst!CODE) Then
Application.Forms("DOCquery").Controls("txtFNAME")
= rst!A_FNAME
Application.Forms("DOCquery").Controls("txtLNAME")
= rst!A_LNAME
Application.Forms("DOCquery").Controls("txtHSTRE1")
= rst!A_HSTRE1
Application.Forms("DOCquery").Controls("txtHSTRE2")
= rst!A_HSTRE2
Application.Forms("DOCquery").Controls("txtHCITY")
= rst!A_HCITY
Application.Forms("DOCquery").Controls("txtHSTATE")
= rst!A_HSTATE
Application.Forms("DOCquery").Controls("txtHZIP") =
rst!A_HZIP
Application.Forms("DOCquery").Controls("txtCODE") =
rst!CODE
Application.Forms("DOCquery").Controls("txtDESC") =
rst!DESC

' Here we are actually inputing the data in to the doc

docLetter.FormFields("dFNAME").Result =
Application.Forms("DOCquery").Controls("txtFNAME")
docLetter.FormFields("dLNAME").Result =
Application.Forms("DOCquery").Controls("txtLNAME")
docLetter.FormFields("dHSTRE1").Result =
Application.Forms("DOCquery").Controls("txtHSTRE1")
' docLetter.FormFields("dHSTRE2").Result =
Application.Forms("DOCquery").Controls("txtHSTRE2")
docLetter.FormFields("dHCITY").Result =
Application.Forms("DOCquery").Controls("txtHCITY")
docLetter.FormFields("dHSTATE").Result =
Application.Forms("DOCquery").Controls("txtHSTATE")
docLetter.FormFields("dZIP").Result =
Application.Forms("DOCquery").Controls("txtHZIP")
docLetter.FormFields("dFNAME2").Result =
Application.Forms("DOCquery").Controls("txtFNAME")
docLetter.FormFields("dLNAME2").Result =
Application.Forms("DOCquery").Controls("txtLNAME")
docLetter.FormFields("dCODE").Result =
Application.Forms("DOCquery").Controls("CODE")
docLetter.FormFields("dPOSITION").Result =
Application.Forms("DOCquery").Controls("DESC")

'make the Word Application object visible
appWord.Visible = True
'release the memory allocated to the application
Set appWord = Nothing
'close the form
DoCmd.Close

Exit Sub
End If
rst.MoveNext
Loop


HTH Ivan
 

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