PDA

View Full Version : Batch insert pictures with filenames as captions



patrinos
06-29-2010, 07:25 AM
Hello everyone, this is my first post here. Sorry if this has been answered before, I searched the forum but didn't find a relevant post, or maybe it is entitled otherwise.

I'm trying to find (I have no VB knowledge to build one myself :dunno) a macro to use with Word 2003 or 2007, that will automatically caption the pictures I insert into word with their filename.

I have found a macro that does exactly that for one file, it adds a caption below the image with it's filename, I'm pasting it below.

Sub InsertPicture2()
Dim txtPhotoPath As String
With Dialogs(wdDialogInsertPicture)
If .Show Then
txtPhotoPath = WordBasic.FilenameInfo$(.Name, 0)
End If
Selection.InsertAfter vbCr & txtPhotoPath
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End Sub

If multiple pictures are selected, the above script will add only the last pictures filename.

BUT I want to be able to do the same thing while inserting MANY pictures at once (multiple selection).

The selection of pictures could be either Ctrl-Clicking at the "Insert>Picture> From File" dialog, or even a whole folder, I do not mind, which ever is easier.

Thank you for your help.

fumei
06-29-2010, 09:23 AM
This may, or may not help. It does not use Selection. It is hard to say because you have not given enough detail.
Sub PicWithCaption()
Dim file
Dim path As String
path = "C:\Gerry\"
file = Dir(path & "*.jpg")

CaptionLabels.Add Name:="Filename"
Do While file <> ""
With Selection
.EndKey Unit:=wdStory
.InlineShapes.AddPicture FileName:=path & file, _
LinkToFile:=False, SaveWithDocument:=True
.InsertAfter vbCrLf & vbCrLf
.Collapse 0
.MoveLeft Unit:=wdCharacter, Count:=1
.Style = "Caption"
.Text = path & file
End With
file = Dir()
Loop
Selection.EndKey Unit:=wdStory
End Sub
This takes ALL the JPG files in a folder (in this case c:\gerry) and:

1. inserts them at the last cursor (Selection) point.
2. write the full path/name below the inserted image.
3. inserts a paragraph mark
4. repeats 1 - 3 until there are no more JPG in the given folder.

You do not state WHERE you want the image files. I put them starting at the current Selection point.

You do not state what, if anything, you want in between them. I put an empty paragraph - which is bad form BTW.

fumei
06-29-2010, 09:24 AM
BTW: notice I do NOT use the Reference > Caption functionality. That is because Captions have NO real relationship to what they are "captioning".

patrinos
06-29-2010, 11:00 AM
Gerry thank you very very much!!

It works perfectly, it is exactly what I wanted. I should have mentioned the pictures could be placed anywhere, actually the whole document includes only the pictures. And the paragraph is perfect too.

If it would be possible to select the folder by dialox instead of fixing it in the code, it would be more than perfect.

Oh, and also, how can I tell the code to look not only for jpg, but also png and tif?

Thank you again.

fumei
06-29-2010, 11:43 AM
As written you would have to do png and tif independently. That is:

all jpg, then
all png, then
all tif

or in whatever order you like. The Dir function processes all files of one type. So you could do all png, then tif, then jpg. But not one png, then a jpg. then a jpg, then a tif.

This is because Dir does not look for anything. You tell it to process specific parameters.

That being said, it IS possible to do such a thing by actioning through the Dir, all files (regardless of extension) and then testing/filtering on the extension.
Sub PicWithCaption()
Dim file
Dim path As String
path = "C:\Gerry\"
file = Dir(path & "*.*") ' ALL files

CaptionLabels.Add Name:="Filename"
Do While file <> ""
' test for extension
If UCase(Right(file, 3)) = "PNG" Or _
UCase(Right(file,3)) = "TIF" Or _
UCase(Right(file,3)) = "JPG" Then
With Selection
.EndKey Unit:=wdStory
.InlineShapes.AddPicture FileName:=path & file, _
LinkToFile:=False, SaveWithDocument:=True
.InsertAfter vbCrLf & vbCrLf
.Collapse 0
.MoveLeft Unit:=wdCharacter, Count:=1
.Style = "Caption"
.Text = path & file
End With
End If
file = Dir()
Loop
Selection.EndKey Unit:=wdStory
End Sub
The use of UCase is to cover possible things like: png, PNG, tif, TIF etc.

Are you following this?

The Dir processes ALL files, regardless of extension, but then there is a test - if the last three characters are PNG, TIF, JPG - THEN do the insert of that file.

As for selecting the folder by dialog, yes this is possible. Try doing a search for this kind of thing.

patrinos
06-29-2010, 11:40 PM
THANK YOU for your time & expert help.
Very much appreciated.
patrinos