-
Jul 8th, 2009, 03:39 AM
#1
[FAQ's: OD] How to execute a Word MailMerge document
Continued from "How to create a Word MailMerge Document"
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
Code:
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|