working with MailMerge Object

E

Ecos

Hello All,
I am using the following code to create individual MM object for each record
in my datasource. My goal is to be able to save each MM object as a file
instead of just opening it in wdSendToNewDocument. Any ideas? I am a
newbie at scripting so be gentle. Thanks!
Matt
***Code*********************
Sub MMerge()

'Create MM object
Set myMerge = ActiveDocument.MailMerge

For i = 1 To 10
'If MM object has valid Data Source, make it consist of the first 2
records
If myMerge.State = wdMainAndSourceAndHeader Or _
myMerge.State = wdMainAndDataSource Then
With myMerge.DataSource
.FirstRecord = i
.LastRecord = i
End With
End If

'Set the destination property of the MM object to the Printer and
execute it
With myMerge
.Destination = wdSendToNewDocument
.Execute
End With
Next i

End Sub
***EndCode************************
 
E

Ecos

Well, I came up with a solution to my mail merge problem. It is not pretty
and the code will probably make some people cringe, but I am not a code pro
so it stands. This code solved my problem of mail mergeing a document into
pages 1412 individual 6 page files each saved under a specific id #. I have
seen other people wondering how to split a mailmerge file into seperate
documents, this is how I did it and so far it seems to work.. I appreciate
any help on how to make this more efficient and/or readable.
Thanks
*************************
Sub MMerge()

Dim CRSNUM As String
Dim cntACCESS As New ADODB.Connection
Dim rstCRSNUM As New ADODB.Recordset
Dim strSQLAttributes As String

'Create a ADO connection to get CRSNUM for file name
cntACCESS.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = YourDB.mdb;"

strSQLAttributes = "SELECT ID FROM YOUR_TABLE;"

'Open ID Num recordset
With rstCRSNUM
.CursorLocation = adUseClient
.Open strSQLAttributes, cntACCESS, adOpenDynamic, adLockOptimistic
End With

rstCRSNUM.MoveFirst


'Create MM object
Set myMerge = ActiveDocument.MailMerge

'cycle through each Merge doc and save it using the current CRSNUM record,
close is and loop
For i = 1 To rstCRSNUM.RecordCount

If myMerge.State = wdMainAndSourceAndHeader Or _
myMerge.State = wdMainAndDataSource Then
With myMerge.DataSource
.FirstRecord = i
.LastRecord = i
End With
CRSNUM = rstCRSNUM("CRSNUM")
'MsgBox CRSNUM & " , " & myMerge.DataSource.LastRecord
End If


'Set the destination property of the MM object to the NewDoc and Execute
it
With myMerge
.Destination = wdSendToNewDocument
.Execute
End With

'Save new doc as ID num .doc and close it
ActiveDocument.SaveAs FileName:="C:/" & CRSNUM & ".doc", _
FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True,
WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:= _
False
ActiveDocument.Close

rstCRSNUM.MoveNext

Next i
rstCRSNUM.Close
cntACCESS.Close
End Sub
************************************
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RWNvcw==?=,
I have
seen other people wondering how to split a mailmerge file into seperate
documents
There's a solution on my website, and a different one by Doug Robbins on
the word.mvps.org site...

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

Similar Threads


Top