Automating Word Mailmerge

S

Sherri

Can someone please help me. I have been trying to automate a mailmerg
for weeks now and I am still having problems. I can get the whol
merge to work however, I need to just do a merge on one record of a cs
file. I am using vbscript and I am having no luck. If someone coul
point me in the right direction...maybe with some sample code tha
would be great!!!! I would owe you my first born!!!
 
C

Cindy M -WordMVP-

Hi Sherri,
I have been trying to automate a mailmerge
for weeks now and I am still having problems. I can get the whole
merge to work however, I need to just do a merge on one record of a csv
file. I am using vbscript
1. Version of Word involved

2. Please post the code you have working

3. Please describe what you've tried, unsuccessfully, so that we don't go
over ground already covered

4. Please give an example of selection criteria. How should the mail
merge know which record is to be used?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

Sherri

Word is 2003

Here is the working code that will perform the mailmerge:

Dim Doc
Dim wrdApp

set wrdApp = CreateObject("Word.Application")
set Doc = CreateObject("Word.Document")
Set Doc = GetObject("H:\samples\New samples\merge.doc")

Doc.Mailmerge.execute

wrdApp.Visible = True


Here is the code where I try to pull a record from the csv file tha
doesn't work:

Dim Doc
Dim wrdApp
Dim dsmain
Dim numrecord
Dim DocMM

set wrdApp = CreateObject("Word.Application")
set Doc = CreateObject("Word.Document")
Set Doc = GetObject("H:\samples\New samples\merge.doc")
Set dsmain = Doc.mailmerge.datasource
numrecord = 0

Set DocMM.ViewMailMergeFieldCodes = False
if dsMain.FindRecord(Find=4444, Field+"Receipt") = True Then
numRecord = dsMain.ActiveRecord
msgbox("4444 Found at " & numrecord)
End If

Doc.Mailmerge.execute

wrdApp.Visible = True

The csv file looks like this:

Receipt, Date,Name,Address,City,St,Zip,DonationAmt,Salutation
1111,"May 25, 2005",John Smith,10 East Mai
Str,Anytown,PA,17320,1000,John
2222,"May 25, 2005",Ann Anderson,23 New Rd,Anytown,PA,17320,5000,Ms
Anderson
3333,"May 25, 2005",Mike Williams,78 West St.,Anytown,PA,17320,300,Mr
Williams
4444,"May 25, 2005",Barb Smith,1 Smith Rd,Anytown,PA,17320,10000,Barb


Again any help is greatly appreciated:)

Sherr
 
C

Cindy M -WordMVP-

Hi Sherri,
set wrdApp = CreateObject("Word.Application")
set Doc = CreateObject("Word.Document")
Set Doc = GetObject("H:\samples\New samples\merge.doc")
this looks suspect to me. You're creating a lot of object
instances, here, that you aren't really using in your code.
You also don't show whether you're destroying any of them
correctly; if you're not, you're opening the door to memory
leaks.

EITHER use just the GetObject on a Word document (doesn't
always work). OR CreateObject for the Word.Application,
then open a document on that:
Set doc = wrdApp.Documents.Open("filename")

And when you're finished, clean up:
Set doc = Nothing
Set wrdApp = Nothing

As to finding the record: There are some problems with
FindRecord, but before we get into that...

if dsMain.FindRecord(Find=4444, Field+"Receipt") = True

What is: Field+"Receipt" supposed to be? You don't define
Field anywhere in the code, that I can see.

Also, you haven't described HOW it's not working. Are you
getting an error message? If yes, what is the exact text?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top