Solved

Drive Mail Merge from excel Macros (using Embedded Docx Templates)

Posted on 2013-05-13
8
908 Views
Last Modified: 2013-05-15
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
0
Comment
Question by:PortletPaul
  • 5
  • 3
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39164208
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
0
 
LVL 48

Author Comment

by:PortletPaul
ID: 39164597
:) 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
0
 
LVL 48

Author Comment

by:PortletPaul
ID: 39164692
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
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39165245
The code that you have shown on the sheet expects the main documents  to be in the same folder as the workbook.

Your worksheet has two embedded workbooks and two buttons, so you could have a generic merge procedure called by individualised macros, each assigned to the appropriate button.
Sub DoMerge1 () 'assigned to Button1
     DoMerge 1
End Sub

Sub DoMerge2() 'assigned to Button2
     DoMerge 2
End Sub

Sub DoMerge(iObjNo As Integer)
    Dim wdApp As Word.Application
    Dim wdMainDoc As Word.Document
    Dim wdResultsDoc As Word.Document
    ActiveWorkbook.Worksheets(1).OLEObjects(iObjNo).Activate
    Set wdMainDoc = ActiveWorkbook.Worksheets(1).OLEObjects(iObjNo).Object
    Set wdApp = wdMainDoc.Application
   
    With wdMainDoc.MailMerge
        .Destination = wdSendToNewDocument
        .Execute
    End With
    wdMainDoc.Close wdDoNotSaveChanges
End Sub

Open in new window

0
 
LVL 48

Author Comment

by:PortletPaul
ID: 39166646
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?
0
 
LVL 48

Author Comment

by:PortletPaul
ID: 39167140
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:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28128386.html
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 500 total points
ID: 39167365
You have several options to match the button to the object.

As you supposed the shapes indexing, while conforming to the rule that the numbering follows the order in which the shapes were added, is not easy to manage.

You could use the shapes names or ID, but you need further coding to determine or to set them. For this it would be helpful to select the object and use
Debug.Print Selection.ShapeRange(1).Name

Another suggestion is to walk through the shapes and to get their positions to see which Word object shape is to the left of the button.
0
 
LVL 48

Author Closing Comment

by:PortletPaul
ID: 39167544
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now