import multiple photos from a folders then resize

jaelh

New Member
Joined
Mar 2, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am using the following code to insert 1 image from a specific folder into a selected cell then resize it.

Please could you help me in the following:

1- select multiple folders to import the photos from each instead of 1 folder

2- resize each photo inserted into a range of cells for example (A4:B10)

for example

Photo 1 ... (A4:B10)

photo 2 ... (C4:D10)

Photo 3 .. (E4:F10)

and so on

Thanks in advance

VBA Code:
Sub InsertPicAndResizeToCell()

Dim vPics
Dim iPic As Integer
vPics = Application.GetOpenFilename("All image files (*.JPG;*.BMP;*.PNG),*.JPG;*.BMP", MultiSelect:=True)
If TypeName(vPics) = "Boolean" Then Exit Sub ' cancelled

Dim oNewPic As Shape
Dim Pic1 As Range

'cell or range of cells where the picture should be inserted:
Set Pic1 = ActiveWindow.RangeSelection

For iPic = LBound(vPics) To UBound(vPics)

  'Insert the picture:
  Set oNewPic = ActiveSheet.Shapes.AddPicture(Filename:=vPics(iPic), LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
  Left:=Pic1.Left + 0, Top:=Pic1.Top + 0, Width:=Pic1.Height, Height:=Pic1.Height)

  'Maintain original aspect ratio and set to full size
  oNewPic.LockAspectRatio = msoTrue
  oNewPic.ScaleHeight factor:=1, RelativeToOriginalSize:=msoTrue
  oNewPic.ScaleWidth factor:=1, RelativeToOriginalSize:=msoTrue

  '    'Resize the picture to fit in the destination cells
  If (oNewPic.Width / oNewPic.Height) < (Pic1.Width / Pic1.Height) Then
  oNewPic.Width = Pic1.Width + 0
Else:  oNewPic.Height = Pic1.Height - 1000
  
  End If
  Set Pic1 = Pic1.Offset(1)  ' replace Sheet1.ComboBox1 with reference to your combobox
Next


End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this macro:
VBA Code:
Option Explicit

Public Sub Insert_Images_from_Folders()

    Dim folders() As String, foldersCount As Long
    Dim showOK As Boolean
    Dim fileName As String
    Dim i As Long
    Dim pic As Shape
    Dim picRange As Range
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        foldersCount = 0
        Do
            .Title = "Select images folder " & foldersCount + 1 & " or click Cancel to " & IIf(foldersCount = 0, "exit macro", "insert images")
            showOK = .Show
            If showOK Then
                ReDim Preserve folders(0 To foldersCount)
                folders(foldersCount) = .SelectedItems(1) & "\"
                foldersCount = foldersCount + 1
            End If
        Loop Until Not showOK
    End With
    If foldersCount = 0 Then Exit Sub
    
    Set picRange = ActiveSheet.Range("A4:B10")
    
    For i = 0 To UBound(folders)
        fileName = Dir(folders(i) & "*.*")
        While fileName <> vbNullString
            If InStr(1, "|.jpg|.png.|.bmp|", "|" & Mid(fileName, InStrRev(fileName, ".")) & "|", vbTextCompare) Then
                With picRange
                    Set pic = .Worksheet.Shapes.AddPicture(fileName:=folders(i) & fileName, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
                                                           Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
                    Set picRange = .Offset(, .Columns.Count)
                End With
            End If
            fileName = Dir
        Wend
    Next
    
End Sub
 
Upvote 0
Try this macro:
VBA Code:
Option Explicit

Public Sub Insert_Images_from_Folders()

    Dim folders() As String, foldersCount As Long
    Dim showOK As Boolean
    Dim fileName As String
    Dim i As Long
    Dim pic As Shape
    Dim picRange As Range
   
    With Application.FileDialog(msoFileDialogFolderPicker)
        foldersCount = 0
        Do
            .Title = "Select images folder " & foldersCount + 1 & " or click Cancel to " & IIf(foldersCount = 0, "exit macro", "insert images")
            showOK = .Show
            If showOK Then
                ReDim Preserve folders(0 To foldersCount)
                folders(foldersCount) = .SelectedItems(1) & "\"
                foldersCount = foldersCount + 1
            End If
        Loop Until Not showOK
    End With
    If foldersCount = 0 Then Exit Sub
   
    Set picRange = ActiveSheet.Range("A4:B10")
   
    For i = 0 To UBound(folders)
        fileName = Dir(folders(i) & "*.*")
        While fileName <> vbNullString
            If InStr(1, "|.jpg|.png.|.bmp|", "|" & Mid(fileName, InStrRev(fileName, ".")) & "|", vbTextCompare) Then
                With picRange
                    Set pic = .Worksheet.Shapes.AddPicture(fileName:=folders(i) & fileName, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
                                                           Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
                    Set picRange = .Offset(, .Columns.Count)
                End With
            End If
            fileName = Dir
        Wend
    Next
   
End Sub

Thank you, unfortunately the code always opens the file dialog and nothing else
 
Upvote 0
Look at the title of the folder picker. You are meant to select one folder and click OK each time the folder picker is displayed, and when all the folders have been selected click Cancel to insert the images from the selected folder(s). If you click Cancel the first time the folder picker is displayed, thus not selecting a folder, the macro will exit.
 
Upvote 0

Forum statistics

Threads
1,214,427
Messages
6,119,419
Members
448,895
Latest member
omarahmed1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top