Drive Mail Merge from excel Macros (using Embedded Docx Templates)
I have an idea for a productivity aid which involves embedding Word (2010) Documents into Excel (2010), then behind a macro button the macro chooses the relevant Word Template and proceeds to populate it with data.
Basically I want to drive Word’s “mail merge” from Excel so that the correct Word templates are retained in a single Excel workbook for distribution.
Is this possible?
Can someone point me to (or provide) a sample macro that will grab an embedded document, then start a “mail merge” using that document as the format template.
Conceptual only Excel workbook attached. Assuming what I’m after is possible I’m not after a fully developed macro, just a stub to show how I would get started.
In Word mail merge, the usual procedure is to create a 'main' document that contains the path to the datasource and the appropriate merge fields. There is a mail merge wizard to help you through the steps. This is then saved, so that whenever the necessary, the document can be opened and a fresh set of letters, emails or faxes can be produced.
This code anticipates this, though I notice that the embedded documents aren't actually set up as merge documents, so the code aborts when the MailMerge bit is reached.
Sub DoMerge()
Dim wdApp As Word.Application
Dim wdMainDoc As Word.Document
Dim wdResultsDoc As Word.Document
ActiveWorkbook.Worksheets(1).OLEObjects(1).Activate
Set wdMainDoc = ActiveWorkbook.Worksheets(1).OLEObjects(1).Object
Set wdApp = wdMainDoc.Application
With wdMainDoc.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
wdMainDoc.Close wdDoNotSaveChanges
End Sub
PortletPaul
ASKER
:) yes I know the usual method is start with Word, then Excel as data source
that's why I'm asking if the reverse is feasible
Neither of the embedded docs in the sample are "setup" - it's conceptual
I want to operate this way as there will be several (simple) word templates, but only one (complex) workbook: the information will come from the workbook so the workflow is more logically controlled in the workbook. Plus I need to distribute this - it will be far easier if all parts are in one file instead of something like a zipped folder (although if that's the best route please tell me so).
You can assume the word templates would have merge tags in them, what I'm really after is:
how to 'unhook' the embedded document to file, then
get that file to start "mail merge"
This code anticipates this, though I notice that the embedded documents aren't actually set up as merge documents, so the code aborts when the MailMerge bit is reached.
Sub DoMerge()
Dim wdApp As Word.Application
Dim wdMainDoc As Word.Document
Dim wdResultsDoc As Word.Document
ActiveWorkbook.Worksheets(
Set wdMainDoc = ActiveWorkbook.Worksheets(
Set wdApp = wdMainDoc.Application
With wdMainDoc.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
wdMainDoc.Close wdDoNotSaveChanges
End Sub