Using VBA to perform Microsoft Word mail merge give you the ultimate control and alleviates the need to jump to
Microsoft Word to perform the mail merge.
To accomplish this task you'll need to create a Word template document with bookmarks inserted in the database field
locations. These bookmarks will represent the variable part of the document.
Next create an Access form with a button called Enveloppe. And, create an on click event as shown below.
You'll need to customized the PathDocu line to match you particular setup.
Private Sub Enveloppe_Click()
Dim MyWord As Word.Application
Dim PathDocu As String
If Me.NomPers <> "" And Me.Prénom <> "" Then
Set MyWord = New Word.Application
PathDocu = "C:\Documents and Settings\BENOIT\Mes documents\Word\IFFC\BaseDeDonnée\Documents\"
With MyWord
.Documents.Open (PathDocu & "Env22x10.doc")
'.ActiveDocument.Bookmarks("date").Range.Text = & _
Format(Date, "dd mmmm yyyy")
.ActiveDocument.Bookmarks("nom").Range.Text = Me.NomPers
.ActiveDocument.Bookmarks("prénom").Range.Text = Me.Prénom
If Me.Adresse <> "" Then
MyWord.ActiveDocument.Bookmarks("adresse").Range.Text =
& _
Me.Adresse
If Me.Adresse2 <> "" Then
MyWord.ActiveDocument.Bookmarks("adresse2").Range.Text & _
= Me.Adresse2
If Me.CodePostal <> "" Then
MyWord.ActiveDocument.Bookmarks("codepostal").Range.Text &_
= Me.CodePostal
If Me.Ville <> "" Then MyWord.ActiveDocument.Bookmarks("ville").Range.Text =Me.Ville
If Me.Pays <> "" Then MyWord.ActiveDocument.Bookmarks("pays").Range.Text = Me.Pays
' si vous désirez utiliser plusieurs fois les mêmes données, vous devez
' créer des signets différents
.Visible = True
End With
DoEvents
Set MyWord = Nothing
End If
On Error GoTo Err_Enveloppe_Click
Exit_Enveloppe_Click:
Exit Sub
Err_Enveloppe_Click:
MsgBox Err.Description
Resume Exit_Enveloppe_Click
End Sub
Contributed by Benoit Mann