RobDog888
Jul 8th, 2009, 03:39 AM
Continued from "How to create a Word MailMerge Document (http://www.vbforums.com/showthread.php?t=402097)"
When you have a Word MailMerge document already setup you may find yourself needing to programmatically execute the MailMerge from your program. We can do this as well as other tasks on the MailMerge.
Part 1: Execute a MailMerge document to a new document or straight to a printer etc...
Word 2000 - 2007 and VB6
Option Explicit
'Add a reference to Microsoft Word xx.0 Object Library
'Or Late Bind without a reference and change all objects to type Object
'and declare all constants explicitly since there is no reference to eval them
Private Sub Command1_click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oMerge As Word.MailMerge
Dim oResult As Word.Document
Set oApp = CreateObject("Word.Application")
'Open a Word MailMerge document
Set oDoc = oApp.Documents.Add("C:\Development\Word Mailmerge\RobDog888 FAQ - Word Mailmerge.doc", False, wdNewBlankDocument, False)
'Instanciate a MailMerge object
Set oMerge = oDoc.MailMerge
With oMerge
'Set the source where your data will be coming from
.OpenDataSource "C:\Development\Word Mailmerge\VB Forums.mdb"
'Specify the output destination. ie: a new document or a printer etc
.Destination = wdSendToNewDocument 'OR wdSendToPrinter if you just want to print
.Execute
End With
'Do stuff with the document if you want via the oResult document object
Set oResult = oApp.ActiveDocument 'This is your merged document (if shown).
You can show the document if you are going to allow the use to edit or ???
oApp.Visible = True
'...
'Close and release ref'd objects saving them if you want by changing the argument to True
oResult.Close SaveChanges:=False
Set oResult = Nothing
Set oMerge = Nothing
oDoc.Close SaveChanges:=False
Set oDoc = Nothing
oApp.Quit SaveChanges:=False
Set oApp = Nothing
End Sub
Continued in Part 2 coming soon.
When you have a Word MailMerge document already setup you may find yourself needing to programmatically execute the MailMerge from your program. We can do this as well as other tasks on the MailMerge.
Part 1: Execute a MailMerge document to a new document or straight to a printer etc...
Word 2000 - 2007 and VB6
Option Explicit
'Add a reference to Microsoft Word xx.0 Object Library
'Or Late Bind without a reference and change all objects to type Object
'and declare all constants explicitly since there is no reference to eval them
Private Sub Command1_click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oMerge As Word.MailMerge
Dim oResult As Word.Document
Set oApp = CreateObject("Word.Application")
'Open a Word MailMerge document
Set oDoc = oApp.Documents.Add("C:\Development\Word Mailmerge\RobDog888 FAQ - Word Mailmerge.doc", False, wdNewBlankDocument, False)
'Instanciate a MailMerge object
Set oMerge = oDoc.MailMerge
With oMerge
'Set the source where your data will be coming from
.OpenDataSource "C:\Development\Word Mailmerge\VB Forums.mdb"
'Specify the output destination. ie: a new document or a printer etc
.Destination = wdSendToNewDocument 'OR wdSendToPrinter if you just want to print
.Execute
End With
'Do stuff with the document if you want via the oResult document object
Set oResult = oApp.ActiveDocument 'This is your merged document (if shown).
You can show the document if you are going to allow the use to edit or ???
oApp.Visible = True
'...
'Close and release ref'd objects saving them if you want by changing the argument to True
oResult.Close SaveChanges:=False
Set oResult = Nothing
Set oMerge = Nothing
oDoc.Close SaveChanges:=False
Set oDoc = Nothing
oApp.Quit SaveChanges:=False
Set oApp = Nothing
End Sub
Continued in Part 2 coming soon.