Friday, April 25, 2014

Inserting Lots of Images into Word Documents

Typically our reports have appendices with hundreds of images in them. Inserting each one individually, formatting, and adding a caption is impractical. This post discusses a method using macros that accomplishes this task.

Create your images in a directory using a sequential naming scheme. Put any identifying information in the file name that you want to show up in the caption. Multiple "tokens" of information can be in the file name as long as they are delimited by a common character such as dash or underscore. Later I'll show how the macro splits each file name and extracts the tokens for use in the caption.


Open you MS Word file and select the developer tab. If it is not shown, you can make it visible by going to File, Options, Customize Ribbon and checking the Developer checkbox in the right most list box. From the developer tab, click Visual Basic and the right click on Project (your filename) in the window to the left and select Import file. Import each file in the below zip file after extracting to a folder of your choice. The two files you will import are RunInsertPicturesGUI.bas and GUI.frm.



Back in MS Word from the developer tab click the Macros button and run the macro titled "RunInsertPicturesGUI".


The following dialog will open. Customize the picture caption using the tokens extracted from the filename. Each token found will replace [X] with the integer in the brackets signifying the token to use. Also specify the delimiter used to seperate tokens in the filenames. Finally, set the image height. I recommend manually inserting the first image to determine what height results in a good overall fit. The textbox in the Test area is for entering a filename to confirm the resulting caption is what you intended. After selecting the images, each will be inserted and the form will close.


Note that the captions will include a chapter number by default. If your have not set up multi-level lists linked to headers, the captions will show the error. You can remove the chapter number in all captions after importing the images by right clicking on any image and selecting Insert Caption. Click on the Numbering button and then uncheck Include chapter number. Then click OK and Close. Finally, update all your captions by selecting all (Control+A) and then pressing F9.

Below is the final result.



InsertPicturesMacros.zip