Solved

Setting up VB6 Mail Merge

Posted on 2011-02-17
6
257 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now