Setting up VB6 Mail Merge

I am adding ome functionailty to a vb6 system setup years ago. I need to add a mailmerge functionality. How do i setup a mailmerge document in word and VB6?
Who is Participating?
CluskittConnect With a Mentor Commented:
First, you need to set up the fields in a word file:

1- Insert-> Quick pieces (translation?) -> Field
2- mailmerge type, use the name of the field.
3- Repeat until all are done.

If you want to do it manually, just go to mailings and you can use the wizard, as long as your query is fully defined. If not, what you have to do is a bit of VBA. Like:

Dim WDApp, myDoc, svDoc, tmpDoc As Object
Dim db As Database
Dim rs As DAO.Recordset

Set db = CurrentDb()
sFile = "C:\Mypath\Myfile.docx"
Set WDApp = CreateObject("Word.Application")
WDApp.Application.Visible = True
Set myDoc = WDApp.Documents.Open(sFile)
CurrentDb.QueryDefs("qryMerge").SQL="SELECT field1, field2 FROM table WHERE field1=" & txtbox1

WDApp.ActiveDocument.MailMerge.OpenDataSource Name:=CurrentDb.Name, ConfirmConversions:=True, ReadOnly:= False, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", WritePasswordTemplate:= "", Revert:=False, Format:=wdOpenFormatAuto, Connection:= "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=CurrentDb.Name;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global " , SQLStatement:="SELECT * FROM `qryMerge`", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
With WDApp.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With

Open in new window

Depends on the office version. 2003 and previous, there's lots of tutorials on the net, and it's pretty straight-forward. After 2007 things changed a bit (like not being able to use parameter queries. Not directly anyway).
ict-torquilclarkAuthor Commented:
I have Word 2007
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

I struggled quite a bit to get a mail merge working because of a parameter query (a query that takes a parameter from, for example, a text box). In the end, I ended up creating the query on the fly, so it wouldn't have any parameters on the merge stage.

See if this helps you:
ict-torquilclarkAuthor Commented:
Ok. How do I actually add merge fields to word?
Ignore svDoc and tmpDoc declarations. My code uses 3 files and shifts data between them.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.