469,271 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

MS Word mail merge automation

I'm trying to do invoke the mail merge functionality of MS Word from a
Python script. The situation is that I have a template Word document,
and a record that I've generated in Python, and I want to output a new
Word .doc file with the template filled in with the record I've
generated.

(To refresh your memory, in Word a mailmerge is achieved by a) under
Tools -> Letters and Mailings, check off Show Mail Merge Toolbar; b)
open a document with template-style variables in the form of
<<FIELD_NAME>>; c) on Toolbar select Open Data Source and select
appropriate Access or Excel or CSV file (with column headers
corresponding to the FIELD_NAME's in your template variables); and then
d) on Toolbar select Merge To New Document to create a new document
with the template variables replaced with the value from the
corresponding column in the data source - here, you can make one
document per row of data source, or just one document for a given row.
Don't forget to save the new document.)

Using various online sources*, I have been able to piece together all
but (what I hope is) the final missing piece, viz., the name of the
method that corresponds to "Merge to New Document" command from within
the Word interface.

Here is the basic code, if anyone can provide the missing piece I (and
others, I suspect) would appreciate it:
import os, win32com.client
doc_template_name = os.path.abspath('template.doc')
data_source_name = os.path.abspath('record23.csv')
doc_final_name = os.path.abspath('record23.doc')

app = win32com.client.Dispatch("Word.Application")
doc = app.Documents.Open(doc_template_name)

#attach data source to template
doc.MailMerge.OpenDataSource(data_source_name)

#merge to new document - THIS RAISES ATTRIBUTE ERROR, HOW TO FIX?
new_doc = doc.MailMerge.MergeToNewDocument()

#save out result
new_doc.SaveAs(doc_final_name)

#cleanup
doc.Close()
new_doc.Close()
app.Quit()


*I found some information here:
http://64.233.161.104/search?q=cache...document&hl=en

and here:
http://www.brunningonline.net/simon/...es/001299.html

as well as here:
http://www.win32com.de/index.php?opt...=86&Itemid=192

I also have the Hammond and Robinson book on Python on Win32 but it
hasn't helped me to discover the method name.

Sep 30 '05 #1
2 11488

Steve M wrote:
I'm trying to do invoke the mail merge functionality of MS Word from a
Python script. The situation is that I have a template Word document,
and a record that I've generated in Python, and I want to output a new
Word .doc file with the template filled in with the record I've
generated.

(To refresh your memory, in Word a mailmerge is achieved by a) under
Tools -> Letters and Mailings, check off Show Mail Merge Toolbar; b)
open a document with template-style variables in the form of
<<FIELD_NAME>>; c) on Toolbar select Open Data Source and select
appropriate Access or Excel or CSV file (with column headers
corresponding to the FIELD_NAME's in your template variables); and then
d) on Toolbar select Merge To New Document to create a new document
with the template variables replaced with the value from the
corresponding column in the data source - here, you can make one
document per row of data source, or just one document for a given row.
Don't forget to save the new document.)

Using various online sources*, I have been able to piece together all
but (what I hope is) the final missing piece, viz., the name of the
method that corresponds to "Merge to New Document" command from within
the Word interface.

Here is the basic code, if anyone can provide the missing piece I (and
others, I suspect) would appreciate it:
import os, win32com.client
doc_template_name = os.path.abspath('template.doc')
data_source_name = os.path.abspath('record23.csv')
doc_final_name = os.path.abspath('record23.doc')

app = win32com.client.Dispatch("Word.Application")
doc = app.Documents.Open(doc_template_name)

#attach data source to template
doc.MailMerge.OpenDataSource(data_source_name)

#merge to new document - THIS RAISES ATTRIBUTE ERROR, HOW TO FIX?
new_doc = doc.MailMerge.MergeToNewDocument()
In VBA, it would be .Execute.
From the VBA Help file:
This example executes a mail merge if the active document is a main
document
with an attached data source.

Set myMerge = ActiveDocument.MailMerge
If myMerge.State = wdMainAndDataSource Then MyMerge.Execute

#save out result
new_doc.SaveAs(doc_final_name)

#cleanup
doc.Close()
new_doc.Close()
app.Quit()


*I found some information here:
http://64.233.161.104/search?q=cache...document&hl=en

and here:
http://www.brunningonline.net/simon/...es/001299.html

as well as here:
http://www.win32com.de/index.php?opt...=86&Itemid=192

I also have the Hammond and Robinson book on Python on Win32 but it
hasn't helped me to discover the method name.


Sep 30 '05 #2
I was finally able to get things working right, so I thought I'd stick
an example here for posterity.

"""An example of a MS Word mail merge using the COM interface.
In order for this script to work you must first run the COM Makepy
utility and select
"Microsoft Word 10.0 Object Library (8.2)" or whatever your version of
Word is.
The template must also be set up with merge fields corresponding to the
data source.
"""
import os, win32com.client

doc_template_name = os.path.abspath('template.doc')
data_source_name = os.path.abspath('record23.csv')
doc_final_name = os.path.abspath('record23.doc')

app = win32com.client.Dispatch("Word.Application")
doc_template = app.Documents.Open(doc_template_name)

mm = doc_template.MailMerge

#attach data source to template
mm.OpenDataSource(data_source_name)

#merge just one record - this step may be redundant
mm.DataSource.FirstRecord = 1
mm.DataSource.LastRecord = 1

#send the merge result to a new document
mm.Destination = win32com.client.constants.wdSendToNewDocument

#merge
mm.Execute()

#apparently app.Documents is like a stack
doc_final = app.Documents[0]

#save our new document
doc_final.SaveAs(doc_final_name)

#cleanup
doc_final.Close()
doc_template.Close()
app.Quit()

Oct 5 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andy | last post: by
1 post views Thread by Rachel | last post: by
3 posts views Thread by Bishman | last post: by
12 posts views Thread by Steve | last post: by
7 posts views Thread by =?Utf-8?B?QmFkaXM=?= | last post: by
7 posts views Thread by giladp1 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.