![]() |
|
#6
|
||||
|
||||
|
Quote:
Once you have the mailmerge main document set up correctly, you can then run the following code: Code:
Sub RunMerge()
Dim strWorkbookName As String
strWorkbookName = ThisWorkbook.FullName
Dim wdapp As New Word.Application
Dim wddoc As Word.Document
With wdapp
'Disable alerts to prevent an SQL prompt
.DisplayAlerts = wdAlertsNone
'Open the mailmerge main document
Set wddoc = .Documents.Open(ThisWorkbook.Path & "\Mail Merge Main Document.docx")
With wddoc
With .MailMerge
'Define the mailmerge type
.MainDocumentType = wdFormLetters
'Connect to the data source
.OpenDataSource Name:=strWorkbookName, AddToRecentFiles:=False, _
Revert:=False, Format:=wdOpenFormatAuto, Connection:="Data Source=" _
& strWorkbookName & ";Mode=Read", SQLStatement:="SELECT * FROM 'Sheet1'"
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
'Define the output
.Destination = wdSendToNewDocument
'Excecute the merge
.Execute
'Disconnect from the data source
.MainDocumentType = wdNotAMergeDocument
End With
'Close the mailmerge main document
.Close False
End With
'Restore the Word alerts
.DisplayAlerts = wdAlertsAll
'Print the output document
.ActiveDocument.PrintOut
'Display Word and the document
.Visible = True
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
mail merge in excel to send pdf forms
|
heyullama | Excel | 7 | 12-01-2013 03:32 PM |
| Excel Mail merge | Joren | Word | 0 | 07-09-2013 07:38 AM |
mail merge with excel and words
|
don0712 | Mail Merge | 1 | 01-29-2013 06:53 PM |
Templates, pictures, Mail Merge and Excel
|
kckay | Word VBA | 1 | 05-17-2012 08:51 PM |
Mail Merge from Excel
|
Kieroncsmith | Mail Merge | 2 | 02-16-2012 10:35 PM |