Avatar of PortletPaul
PortletPaul
Flag for Australia asked on

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.

Thanks.
ps: I’ve not used VBA for a long while, but it’ll come back to me I’m assuming :)
Make-Docs-Embedded-Templates-Fro.xlsx
Microsoft ExcelMicrosoft WordMicrosoft Office

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon
GrahamSkan

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 looks hopeful
http://stackoverflow.com/questions/1357121/executing-word-mail-merge
PortletPaul

ASKER
a new file - now one template has merge tags in it

I would like to achieve this with buttons if possible, each button must invoke a different embedded document

(double-clicking on a document instead might work perhaps, less desirable)
Make-Docs-From-Embedded-Template.xlsx
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PortletPaul

ASKER
excellent, this is looking quite promising, just another dumb question or so:

how do I know which document is 1, 2 , 3 etc
i.e. is it determined the sequence I embed the documents?

what happens if I add (say) 5 documents, then remove the third, and add a replacement?
will the replacement be 3 or 6?

is referring to these by number the only way?
PortletPaul

ASKER
The macros provided are doing fine so far - thanks you - I'd just like to know a wee bit more about referencing those embedded objects (see above)

follow on question if you are interested:
https://www.experts-exchange.com/questions/28128386/Mail-Merge-Generated-Document-How-to-keep-field-in-a-paragraph.html
SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PortletPaul

ASKER
Have to fine some method to 'name' these objects I suspect

even a list of them would be a good start

Regardless, thank you for the great start here - it's looking promising.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.