Thursday, May 05, 2005

Bug in Word 2003 Multiple Image Picture File Import with AutoCaption and a VB Script I Wrote to Import and Caption Multiple Image Files Correctly

Until Microsoft Word XP there was a huge oversight in image import, that I would call a bug, but most would probably just consider a design flaw. In versions of Word prior to XP and 2003 (up to and including 2000) the insert picture command only allowed inserting one image at a time. So, for example, if you went into Insert->Picture->From File… and tried to select more than one image in the file selection dialog, you were out of luck. Thankfully that feature was introduced in Word XP and continues in Microsoft Word 2003. (Incidentally, Microsoft Word 2004 for Mac still does not allow the selection of more than one image at a time under that same menu option, which I consider a serious flaw. This is one example of where the Windows version of Word is superior to the Mac OS X version. And in Word 2004 for Mac, dragging and dropping multiple image files onto a Word document, only results in one of the images being inserted, which I do consider a bug as that is not how any other Mac application behaves.)

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.InlineShapes.AddPicture fileName:= _
file, LinkToFile:=False, SaveWithDocument:=True
Selection.InsertCaption Label:="Figure", TitleAutoText:="", Title:="", _
Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
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.


techwriter said...

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.

Alex said...

I know a lot of interesting softwares which work with word files. But one of them surprised me reason of it restored my old .doc files - can u receive corrupt word files quite simply and free of cost. Above I suggested it some of my friends.

Joo said...

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