Solved

Setting up VB6 Mail Merge

Posted on 2011-02-17
6
264 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:ict-torquilclark
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34917060
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).
0
 
LVL 1

Author Comment

by:ict-torquilclark
ID: 34917084
I have Word 2007
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34917200
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:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26559967.html
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:ict-torquilclark
ID: 34917445
Ok. How do I actually add merge fields to word?
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 34917566
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

0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34917576
Ignore svDoc and tmpDoc declarations. My code uses 3 files and shifts data between them.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question