Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail Merge Command Button.Access 2007

Status
Not open for further replies.

ajolson1964

Programmer
Mar 25, 2008
31
US
I have a command button that, updates data in a table then starts the mail
merge wizard. The user then selects the directory and the name of the file
to merge a presto works great.
Here is that code

Dim StrName As String
' This deletes the content of a table and replaces it with new information
DoCmd.SetWarnings False
DoCmd.OpenQuery ("DeleteGasRockWeeklyReport")
DoCmd.OpenQuery ("AllWeeklyReportinfoToDocument")
DoCmd.SetWarnings True
' *********** Code Start ************
' 'This example starts the Word Mail Merge wizard using the query passed to
it.
StrName = "AllWeeklyReport"
DoCmd.SelectObject acTable, StrName, True
DoCmd.RunCommand acCmdWordMailMerge
Exit Sub
End Sub

I want to take this to the next level. What I want to do is when the user
clicks on the command button; it merges with and opens the document without having to use the wizard.

I have looked and looked for some code to do this but what I find just does not work.

So does anyone have code that I Could add to what I already have that will
allow the user to click the command button and the file open up with the
merged data?

Thanks
Andy

 


Now here is a code I have been trying to get to work. It should do the trick but I get a 'Compile error: Saying: User-Defiened Type not defined. It refers to the Dim MyWord as Word.Applicaion.

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 = "N:\Reports\Gas Rock Weekly Production Reports\"

With MyWord
.Documents.Open (PathDocu & "GasRockWeeklyReport1.docx")
'.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
 
You have to reference the Microsoft Word Object library.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top