Ok, so I needed to put together a report containing several hundred UML diagrams. The diagrams had already been exported in .WMF format, which Word can read, and since I was using Word 2003, I was able to select all of them for import at the same time. I thought my problems had been solved.
However, I also needed Word to AutoCaption (and number) the images, just with a simple caption, like “Figure 10” under each one. So I turned on AutoCaption for importing images (you do this by selecting Insert->Caption… and then clicking AutoCaption in the dialog and then selecting the type of files you want captions to be added to on import.)
Perfect. I had AutoCaption setup to put the word “Figure” and an auto incrementing number after that on every image I imported. I selected my several hundred image files, and told Word to import them. That’s when we found a pretty major bug in Word 2003.
To demonstrate this bug, let’s simplify the task a bit. Let’s say I have three images of cats I want to import and AutoCaption. While I expected, when I imported them to look like this:
Instead, it came out looking like this:
That’s a pretty serious bug as no one would ever want that behavior, and it seems pretty obvious that no one ever tested multiple image file import with AutoCaption on before shipping Word 2003. Since it has been out for over two years now, I don’t have high hopes of it getting fixed anytime soon.
So how did I solve this problem? I wrote a VB script that does the import and captioning for you. I’ve included the source here. Hopefully it will help anyone else who comes across this bug.
Here is the script:
Sub InsertImagesWithCaptions()
Dim fso, d1, file, fileName
Set fso = CreateObject("Scripting.FileSystemObject")
fileName = "C:\Documents and Settings\aric\My Documents\My Pictures\Cats"
Set d1 = fso.GetFolder(fileName)
For Each file In d1.Files
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.TypeParagraph
Selection.InlineShapes.AddPicture fileName:= _
file, LinkToFile:=False, SaveWithDocument:=True
Selection.TypeParagraph
Selection.InsertCaption Label:="Figure", TitleAutoText:="", Title:="", _
Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.TypeParagraph
Next
End Sub
You'll have to customize the fileName path to point to the directory containing the images you want to import, but that's a lot simpler that manually importing them and captioning them.
BTW apparently even though Word 2004 for Mac supports VB macros, it doesn’t understand CreateObject. If anyone knows how to get a directory like this in VB in Word 2004 for Mac OS X, let me know, and I’ll post a Mac version of this script as well.
2 comments:
This is GREAT! I used it in Word 2007. What I really want, though, is to insert the filename of the image, rather than a figure number.
Worked a charm, thanks. For "techwriter" above add a line:
Selection.TypeText file
after the line:
Selection.InlineShapes.AddPicture fileName:= _
file, LinkToFile:=False, SaveWithDocument:=True
Post a Comment