Well, I've already responded to a couple of queries off-list, so it may be
worth just posting it to the the list, although I hope it's not considered
off-topic (well, it does have Stata code in the solution) or offend any
particularly devoted anti-Microsoft people. Here's how I've done it:
Using MS Word mail merge to automate graph imports from Stata:
You need to use Word fields: use Insert->Field to access the field type
keywords (you can see the field codes on screen by toggling alt-F9).
I've set mine up using a table and put field codes into each of 3 cells.
The table approach is a relatively easy way to get the positioning right.
Here is a sample of the 3 field codes that would appear in the 3 cells of
the table:
{INCLUDEPICTURE "../../DATA/MYGRAPHS/{MERGEFIELD "sitename"}_g1.emf"}
{INCLUDEPICTURE "../../DATA/MYGRAPHS/{MERGEFIELD "sitename"}_g2.emf"}
{INCLUDEPICTURE "../../DATA/MYGRAPHS/{MERGEFIELD "sitename"}_g3.emf"}
Note that each field code has the field cose {MERGEFIELD "sitename"} nested
into the INCLUDEPICTURE field code. This approach allows the mail merge to
fill in part of the graph file name. When a mail merge is run (merge to new
document), the tab delimited text data file (that I created in Stata as part
of my graph generating do loop) has a column called "sitename". sitename is
really just part of the name of a graph file I created -- the actual files
for each page have the suffixes _g1, _g2, and _g3. For example, if sitename
is "site1", these fields refer to graph files site1_g1.emf, site1_g2.emf,
and site1_g3.emf. When the mail merge is complete, you need to Edit->Select
All and hit F9 to update all of the fields and see the right graphs. For my
recent project, the mail merge took a one page template and created a 186
page appendix with information and 3 graphs about each of 186 sites (I hope
no one ever prints it out).
In Stata, my do file does something like this:
qui levels site, local(sitelist)
foreach site of local sitelist {
...*prepare stuff for my graphs
graph ......., saving(`site'_g1.gph, replace)
graph export `site'_g1.emf, replace
graph .........., saving(`site'_g2.gph,replace)
graph export `site'_g2.emf, replace
graph .........., saving(`site'_g3.gph,replace)
graph export `site'_g3.emf, replace
}
outsheet sitename othervars... using mergedata.txt
The foreach loop creates the 3 graphs with the common filename stub for each
site. The outsheet command creates the file mergedata.txt that you tell
Word to mail merge from (use as data source) and, in my case, it includes
other information besides the graph name stubs that I want to put on each
page of the merge file ( I have a table of information about each site with
analysis results and the graphs)
Side note: I also set the xsize and ysize of the graphs when I create them
to make them have the right aspect ratio for the table cells that they are
in -- I actually set up a recent project as a 4 row table with the first row
having just
one cell the full page width (for a long time series plot) and about 3
inches high, the second row used for a caption, the third row contained two
equal cells for two squarish plots and the fourth row contains the captions
for those two plots. I set the xsize and ysize to make these graph look
right and not waste much space.
Hope this helps,
Michael Blasnik
[email protected]
----- Original Message -----
From: "Buzz Burhans" <[email protected]>
To: <[email protected]>
Sent: Thursday, June 10, 2004 12:05 PM
Subject: Re: st: Re: Trouble copying graphs to Word from Explorer
> Michael, I would also be very interested in your procedure for inserting
> graphs into Word documents this way, even if only the major steps you
take;
> I would expect to be able to put the details together myself, but having
an
> idea of the main steps you use would be a helpful start.
>
> Thanks,
>
> Buzz Burhans
> >
> >--- Michael Blasnik <[email protected]> wrote:
> > > I recently learned how to use mail merge in Word to grab the name
> > > of the picture file from a text database (which I create in Stata)
> > > and insert it into the includepicture field. This approach allowed
> > > me to create a 186 page appendix to a report that includes three
> > > Stata graphs on each page (yes, 558 different graphs) by just
> > > creating a one page file in Word and issuing a mail merge comand
> > > -- 186 pages are created in about 1 minute.
> > > You can check online forums about Word for details on this or email
> > > me off-list
> > >
> > > Michael Blasnik
> > > [email protected]
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/