Sub PicturesWithCaption()
Dim xFileDialog As FileDialog
Dim xPath, xFile As Variant
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
If xFileDialog.Show = -1 Then
xPath = xFileDialog.SelectedItems.Item(1)
If xPath <> "" Then
xFile = Dir(xPath & "\*.*")
Do While xFile <> ""
If UCase(Right(xFile, 3)) = "PNG" Or _
UCase(Right(xFile, 3)) = "TIF" Or _
UCase(Right(xFile, 3)) = "JPG" Or _
UCase(Right(xFile, 3)) = "GIF" Or _
UCase(Right(xFile, 3)) = "BMP" Then
With Selection
.InlineShapes.AddPicture xPath & "\" & xFile, False, True
.InsertAfter vbCrLf
.MoveDown wdLine
.Text = xFile & Chr(10)
.MoveDown wdLine
End With
End If
xFile = Dir()
Loop
End If
End If
End Sub
Dim xFileDialog As FileDialog
Dim xPath, xFile As Variant
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
If xFileDialog.Show = -1 Then
xPath = xFileDialog.SelectedItems.Item(1)
If xPath <> "" Then
xFile = Dir(xPath & "\*.*")
Do While xFile <> ""
If UCase(Right(xFile, 3)) = "PNG" Or _
UCase(Right(xFile, 3)) = "TIF" Or _
UCase(Right(xFile, 3)) = "JPG" Or _
UCase(Right(xFile, 3)) = "GIF" Or _
UCase(Right(xFile, 3)) = "BMP" Then
With Selection
.InlineShapes.AddPicture xPath & "\" & xFile, False, True
.InsertAfter vbCrLf
.MoveDown wdLine
.Text = xFile & Chr(10)
.MoveDown wdLine
End With
End If
xFile = Dir()
Loop
End If
End If
End Sub
How Can I insert four photos in one page with this? Is it possible?
ReplyDeleteThanks for sharing this trick.
This worked beautifully for close to 70 images
ReplyDeleteThanks for the feedback.
Deletecan you just help me write the name of the picture inside a text box ?
ReplyDeletename coming at bottom is possible to put on Top of photo
ReplyDeletePlease use below code
DeleteSub PicturesWithCaption()
Dim xFileDialog As FileDialog
Dim xPath, xFile As Variant
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
If xFileDialog.Show = -1 Then
xPath = xFileDialog.SelectedItems.Item(1)
If xPath <> "" Then
xFile = Dir(xPath & "\*.*")
Do While xFile <> ""
If UCase(Right(xFile, 3)) = "PNG" Or _
UCase(Right(xFile, 3)) = "TIF" Or _
UCase(Right(xFile, 3)) = "JPG" Or _
UCase(Right(xFile, 3)) = "GIF" Or _
UCase(Right(xFile, 3)) = "BMP" Then
With Selection
.Text = xFile & Chr(10)
.MoveDown wdLine
.InlineShapes.AddPicture xPath & "\" & xFile, False, True
.InsertAfter vbCrLf
.MoveDown wdLine
End With
End If
xFile = Dir()
Loop
End If
End If
End Sub
Hello Techie,
ReplyDeleteThank you so much for your help. It really saved me lots of time and effort so you have really done magic for me. One question, I wanted to paste 2 images per page, so I tried to increase the page layout margin but the image height was not making it possible. So please advise if there's way to paste two images per page. Thank you so much.
Hello Techie, This did not work for me and I am not sure why. I am on Microsoft 10 Office - my files are .jpg but when I go to the file, it doesn't show any images. This macro would really help me out!
ReplyDeleteCheck if your images are actually .JPEG - in that case add this line:
DeleteUCase(Right(xFile, 4)) = "JPEG" Or _
Hello Techie,
ReplyDeleteCan you help me with the code to update the images with the new images which are already there on the Microsoft word
Hi, when I run.. it run until the select folder then after select folder, it look like processing for short while then it went to the VB module and stop... why?
ReplyDeleteThis was easy and worked well for over 131 images. Thank you!
ReplyDeleteI would like to format the caption using the Insert Caption function in Word. My intention is to easily generate a table of figures at the beginning of the report.
ReplyDeleteI believe this is leading me down the right track, but I haven't gotten it to work with your code yet -
. InsertCaption( _Label_ , _Title_ , _TitleAutoText_ , _Position_ , _ExcludeLabel_ )
https://docs.microsoft.com/en-us/office/vba/api/word.selection.insertcaption
Ideally the TitleAutoText would auto populate with the file extension name, but exclude the file type.
Whether or not you're able to help, thank you for providing the work you have already done!
I realized I could have been more precise. When I use the below code and try to generate a Table of Figures it also includes the photos. Any help is appreciated.
ReplyDeleteSub PicturesWithCaption()
Dim xFileDialog As FileDialog
Dim xPath, xFile As Variant
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
If xFileDialog.Show = -1 Then
xPath = xFileDialog.SelectedItems.Item(1)
If xPath <> "" Then
xFile = Dir(xPath & "\*.*")
Do While xFile <> ""
If UCase(Right(xFile, 3)) = "PNG" Or _
UCase(Right(xFile, 3)) = "TIF" Or _
UCase(Right(xFile, 3)) = "JPG" Or _
UCase(Right(xFile, 3)) = "GIF" Or _
UCase(Right(xFile, 3)) = "BMP" Then
With Selection
.InlineShapes.AddPicture xPath & "\" & xFile, False, True
.InsertAfter vbCrLf
.Collapse Direction:=wdCollapseStart
.InsertCaption Label:="Figure", Title:=":", Position:=wdCaptionPositionBelow
.Text = xFile & Chr(10)
.MoveDown wdLine
End With
End If
xFile = Dir()
Loop
End If
End If
You will want to add the following line after .Collapse Direction:=wdCollapseStart:
Delete.MoveDown wdLine
Worked great and saved me masses of tiresome work. Thanks
ReplyDelete