Has anyone had problems inserting a PNG file into a word document, using a VBA Macro?
I have an MS Word document that contains a very large directory listing of image files, inside a table. I’ve been asked to update the document by inserting the corresponding image in front of the name.
Now, if I enter the image manually (using Insert|Image|From File), I’m able to successfully place the PNG image … so I decided to write a quick VBA Macro to insert the image for me. The following is a sample of the code:
Dim myFile As String
Selection.SelectCell
Selection.Copy
myFile = _
Chr(34) & "C:\Documents and Settings\...\Project\Images\" _
& Left(Selection.Text, Len(Selection.Text) - 2) & Chr(34)
Selection.InlineShapes.AddPicture _
FileName:=myFile, LinkToFile:=False, SaveWithDocument:=True
Outcomes:
- Whenever I execute the macro, I get the “Unable to Convert” error dialog, and no image is inserted.
- I even changed the code to invoke the
wdDialogInsertPictureDialog instead, and it worked just fine.
This is very confusing … using a manual process, the insert works, but going with an automated solution, the insert doesn’t work!
Any ideas or suggestions?
I’ve tried the macro several times and it works … it seems that I’m no longer able to re-create the error again. So, I’m going to mark this under the “mysteries of Office VBA” column and leave it as is … this is not a high-priority project, so there is no need for me to continue investigating.
Thanks to Alain and Joel Spolsky for their help.