Solved

Setting up VB6 Mail Merge

Posted on 2011-02-17
6
259 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …

895 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

11 Experts available now in Live!

Get 1:1 Help Now