Problem using DOC.MailMerge

AlphaMu

New member
Local time
Yesterday, 22:37
Joined
Jul 14, 2018
Messages
6
Big problem using MailMerge. I did check some code on the internet but i must not understand it. In desparation i try this code from a book. It doesnt do it the way i would (Using the me.Recordset of a tabular form and passing each row of the recordset to word. Each row represent a tenant his address and the amount due. The code in the book instead of the recordset use a Query. It open a document in word but doesnt fill the letter.docx and there is a little window that keep asking to open a list of tables or views that i have in my application.

Private Sub cmdPublipostage_Click()
Dim oApp As Word.Application
Dim DOC As Word.Document
Dim DBName As String
Const DOCName = "Lettre.docx"


Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Set DOC = oApp.Documents.Open(Application.CurrentProject.Path & "" & DOCName)

DBName = Application.CurrentDb.Name

With DOC.MailMerge
.OpenDataSource Name:=DBName, Connection:="QUERY ReqfrmPublipostage"
.Destination = wdSendToPrinter
.Execute
End With
Exit Sub

End Sub


Thanks for your help
 
I don't use mail merge code like this but can see one obvious issue.
In your Set DOC line, you need a backslash
Code:
Set DOC = Application.CurrentProject.Path & "\" & DOCName
 
remember that you can't use same db as the one you are running the code from.
aside from ridders suggestion you can use SQLStatement in your mailmerge:
Code:
Private Sub cmdPublipostage_Click()
Dim oApp As Word.Application
Dim DOC As Word.Document
Dim DBName As String
Const DOCName = "Lettre.docx"


Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Set DOC = oApp.Documents.Open(Application.CurrentProject.Path & "\" & DOCName)

DBName = Application.CurrentDb.Name

With DOC.MailMerge
.OpenDataSource Name:=DBName, SQLStatement:="Select yourTable.* From yourTable;"
.Destination = wdSendToPrinter
.Execute
End With
Exit Sub

End Sub
 
Do you have a good reason to use word to do your printing rather than an access report? I ask because often a well designed report is far more handy for these types of actions than going through other software.
 
Mark makes a very good point.

I use Albert Kallal's Super Easy Mail Merge utility to run all merge code from within access as Access reports. As the name indicates its 'super easy' to use
 
Colin,

Only times I've had "Word Mail Merge" be the main report generator ACCESS was simply spitting out a spreadsheet that Word could read in for name/address. As is was for things like news letters and notices of schedule change it worked very well.
 
That's not how Albert's code works.
Everything is done from Access - I recommend it strongly
 

Users who are viewing this thread

Back
Top Bottom