vipin.panchal2008
New member
Sub CompressMultiplePictures()
Dim fileNames As Variant
Dim pic As Picture
Dim r As Range
Dim cellAddresses As Variant
Dim i As Long
' Define the target cell addresses for inserting pictures
cellAddresses = Array("B5", "D5", "B9", "D9", "B18", "D18", "B22", "D22", "B31", "D31", "B35", "D35", "B44", "D44")
' Prompt the user to select multiple image files
fileNames = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
Title:="Please select image files...", MultiSelect:=True)
If IsArray(fileNames) Then
' Loop through each selected file and each cell address
For i = LBound(fileNames) To UBound(fileNames)
' Set the active cell reference (r) to the current cell address
Set r = ActiveSheet.Range(cellAddresses(i))
' Insert the selected image into the "Snaps" worksheet and assign it to the pic variable
Set pic = Worksheets("Snaps").Pictures.Insert(fileNames(i))
With pic
.ShapeRange.LockAspectRatio = msoFalse
.Left = r.Left
.Top = r.Top
.Width = r.Width
.Height = r.Height
End With
' Compress the picture
Application.SendKeys "%a~"
Application.CommandBars.ExecuteMso "PicturesCompress"
Next i
End If
End Sub
Dim fileNames As Variant
Dim pic As Picture
Dim r As Range
Dim cellAddresses As Variant
Dim i As Long
' Define the target cell addresses for inserting pictures
cellAddresses = Array("B5", "D5", "B9", "D9", "B18", "D18", "B22", "D22", "B31", "D31", "B35", "D35", "B44", "D44")
' Prompt the user to select multiple image files
fileNames = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
Title:="Please select image files...", MultiSelect:=True)
If IsArray(fileNames) Then
' Loop through each selected file and each cell address
For i = LBound(fileNames) To UBound(fileNames)
' Set the active cell reference (r) to the current cell address
Set r = ActiveSheet.Range(cellAddresses(i))
' Insert the selected image into the "Snaps" worksheet and assign it to the pic variable
Set pic = Worksheets("Snaps").Pictures.Insert(fileNames(i))
With pic
.ShapeRange.LockAspectRatio = msoFalse
.Left = r.Left
.Top = r.Top
.Width = r.Width
.Height = r.Height
End With
' Compress the picture
Application.SendKeys "%a~"
Application.CommandBars.ExecuteMso "PicturesCompress"
Next i
End If
End Sub