VBA code for Mail Merge

L

Lisab

Is it possible? - Does anyone know where I can find the vba code to do a mail
merge from Access to Word? (both 2007)

1.) The Word document is already set up with 3 data fields (txtName,
DateRange, Signature)
2.) I would like to use a query for the merge data. the query output will
change based on user selections. the fields in the query will be used in the
word document (Name, Date, signature) **One document per name to be printed
3.) the code to start the mail merge will reside in a ButtonClick event

Thank You
 
P

PieterLinden via AccessMonster.com

Lisab said:
Is it possible? - Does anyone know where I can find the vba code to do a mail
merge from Access to Word? (both 2007)

1.) The Word document is already set up with 3 data fields (txtName,
DateRange, Signature)
2.) I would like to use a query for the merge data. the query output will
change based on user selections. the fields in the query will be used in the
word document (Name, Date, signature) **One document per name to be printed
3.) the code to start the mail merge will reside in a ButtonClick event

Thank You

Try Albert Kallal's "Super Easy Word Merge"
http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html
 
L

Lisab

OK, this is what I have. Not exactly a mail Merge, but it works.

I want to use the query results to populate the fields in the word document
and print the word document, one record at a time. I don't want to save the
document. This code seems to work.

-------------------
Public Sub MergeWordCertificate(DocumentName As String)
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim Qef As QueryDef
Dim prm As Parameter

Set WordApp = New Word.Application
WordApp.Documents.Open FileName:=DocumentName
WordApp.Visible = False
Set WordDoc = WordApp.ActiveDocument

Set TheDB = CurrentDb
Set QDef = TheDB.QueryDefs("qryClassCertificates3Day")
QDef![Forms!frmClassRoster!cboClassDate] =
Forms![frmClassRoster]![cboClassDate]

Set SourceRS = QDef.OpenRecordset

If SourceRS.RecordCount > 0 Then
SourceRS.MoveFirst
Do Until SourceRS.EOF
With WordDoc
'Take variables and place them into designated fields in WordDoc
.FormFields("txtName").Result = SourceRS!Name
.FormFields("txtTrainingDates").Result = SourceRS!ClassDateRange
.FormFields("txtEndDate").Result = SourceRS!EndDate
.PrintOut Background:=False

End With
SourceRS.MoveNext
Loop
End If

WordApp.Documents.Close
WordApp.Quit ' close the Word application
Set WordDoc = Nothing
Set WordApp = Nothing
 

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