# Automatically insert MMA plots into MS Word?

If I generate a hundred plots in Mathematica, is there an easy way to insert them into a Word file and add a caption?

Currently I am outputting individual images as .emf's with a text file of captions and then using MATLAB to read them in and use their routines for inserting graphics into Word, but since MATLAB has this capability, it seems like Mathematica should have something similar.

My coworker who gave me the MATLAB code snippet to insert the graphics into Word is giving me a bunch of grief... like why don't I just generate the plots in MATLAB.

• TIP: don't use Word. Use Latex instead. You'll thank me later. Jul 11, 2018 at 22:23
• If you don't find a better solution, you could use Matlab commands through mathematica with matlink package: matlink.org Probably faster than what you are doing Jul 11, 2018 at 22:26
• HAHAHA! Yeah, I wish I could use LaTeX for work (I'm using it for personal publishing!), but... you know... stuck with Word in that environment! I'll look into matlink. Thanks! Jul 11, 2018 at 22:37
• Can you post the matlab code? On Mac you can probably automate this using Applescript. In the past I have created a CellEvaluationFunction that writes to word documents from Mathematica using Applescript. If you are on windows I'd like to see what Matlab are doing for any clues as t how this might work in Mathematica Jul 12, 2018 at 0:00
• Also: if you export to EMF, the quality of the figures will be reduced. If you export to PDF and insert to Word (possible on OS X), again you risk reducing the quality. Word is just a crappy format. But I do know very well how and why one ends up being forced to use it for writing papers regardless. Jul 12, 2018 at 11:12

Since I never got a real answer to this question, I'll post the answer that I found and is workig flawlessly for me! Revisiting what matlab was doing passing commands through ActiveX, I realized it was very similar to running VBA scripts in Word, so I wrote a couple functions that save my plots to temp files and writes a single VBA script for reading them in and adding captions, so I can dump a pile of images into a Word file, add the appropriate captions etc. and... presto! An automatic way to insert a bunch of images into Word! I'm finding 360 dpi .png files look perfect! Best!

(* Read this into a new notebook using Get["X:\\mathematica\\WordInsert.m"] *)
BeginWordInsert[path_] := Module[{},
mmamacro = OpenWrite[path <> "\\mma_insert.bas"];
WriteString[mmamacro,
"Attribute VB_Name = \"NewMacros\"\n\n", "Sub mma_insert()\n",
"Attribute Macro1.VB_ProcData.VB_Invoke_Func = \
\"Normal.NewMacros.mmaInsert\"\n",
"'\n", "' ", "mmaInsert1 Macro\n", "'\n", "'\n"];
mmaInsertN = 1;
pathMI = path;];
(* *)
EndWordInsert[] :=
Module[{}, WriteString[mmamacro,"End Sub"]; Close[mmamacro]];
(* *)
WordInsertP[graphic_, title_:"  Caption", IMres_:300,
fileType_: "png"] := Module[{},
fileN =
pathMI <> "\\mmaEP" <> ToString[mmaInsertN] <> "." <> fileType;
Export[fileN, graphic, ImageResolution -> IMres];
WriteString[mmamacro, "Selection.TypeParagraph\n",
If[mmaInsertN > 1,
"Selection.TypeParagraph\nSelection.TypeParagraph\n", ""],
"\"", fileN, "\", ", "LinkToFile:=False, _\n",
"SaveWithDocument:=True\n",
"Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
"Selection.Start = Selection.Start-1\n",
"Selection.InsertCaption Label:=\"Figure\" , _\n",
"Title:=\"  ", title,
"\", Position:=wdCaptionPositionBelow,ExcludeLabel:=0\n",
"Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
"'\n"];
mmaInsertN = mmaInsertN + 1;
];
(* *)
WordInsertT[graphic_, title_: "  Caption", IMres_: 300,
fileType_: "png"] := Module[{},
fileN =
pathMI <> "\\mmaEP" <> ToString[mmaInsertN] <> "." <> fileType;
Export[fileN, graphic, ImageResolution -> IMres];
WriteString[mmamacro, "Selection.TypeParagraph\n",
If[mmaInsertN > 1,
"Selection.TypeParagraph\nSelection.TypeParagraph\n", ""],
"\"", fileN, "\", ", "LinkToFile:=False, _\n",
"SaveWithDocument:=True\n",
"Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
"Selection.Start = Selection.Start-1\n",
"Selection.InsertCaption Label:=\"Table\" , _\n",
"Title:=\"  ", title,
"\", Position:=wdCaptionPositionAbove,ExcludeLabel:=0\n",
"Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
"'\n"];
mmaInsertN = mmaInsertN + 1;
];
session, run \"BeginWordInsert[path]\", where the path string points \
to your working directory.\n\nNote escape characters need to be used \
in the path definition \n(IE. \"X:\\\\mathematica\").\n\nWrapers \
WordInsertP and WordInsertT are for inserting (P)lots and (T)ables \
respectively.\n The insert functions are of the form \
WordInsert[graphic,title,ext], \nwhere graphic is any Mathematica \
graphic object and the title is the string used in Word for the \
Figure or Table caption (needs to be in quotes).\n ext is the file \
extension defining the type of graphic output to use (IE. \"png\").\n\
\nThis package writes a \".bas\" file that can be inported into MS \
Word.\nAll Plots and Tables will be inserted with captions when \
\"mmainsert\" macro is run in Word.\n\nTo end your WordInsert session and finalize the .bas file creation,\n you \
must execute the \"EndWordInsert[]\" function.\n\n
A typical run sequence:\n\nBeginWordInsert[\"X:\\mathematica\"]\nWordInsertP[g1,title1]\nWordInsertP[g2,title2]
WordInsertT[g3,title3]\nEndWordInsert[]\n\n"]


If you start off with a template document containing EMFs you can rename the .docx to .zip, unzip it, replace the EMFs in the media folder. Zip it back up, rename back to .docx and now your document contains your images. This is simple to automate.

Note, the template images need to be all different. If you copy and paste a page 100 times, when the document is saved there will just be one image in the media folder (referenced 100 times) because Word recognises identical images during the Save process.

You can use NETLink interop functions to build a template, or make one in Word - a bit tedious for 100 images but the template can be used over and over again.

• Thanks! That doesn't seem like a great workaround for me since there will be different numbers of graphics each time (sometimes 62 and sometimes 364...). I look into the LaTeX/.pdf to word options because I can write a little awk script to automatically build a LaTeX file and maybe go from there. Jul 13, 2018 at 17:44
• I added my "package" that does this below. Put into a text file and then "Get" in mma and the instructions and example will print to screen. Then just follow directions and use! Works for Figures and Table. 360dpi png look the cleanest to me. May 29, 2019 at 5:00
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.45262226462364197, "perplexity": 5740.365052569598}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-33/segments/1659882572033.91/warc/CC-MAIN-20220814113403-20220814143403-00081.warc.gz"}