Be the first user to complete this post
|
Add to List |
Excel-VBA : Insert Multiple Images from a Folder to Excel Cells
Say you have many images in a folder and you want to insert all these images in your excel work book, one image in one cell. You can do it manually, insert and resize the image and drag it to the particular cell, but think of a scenario where you have more than 100 images or may be more. I am sure you don't want to do it manually. IF you are looking for solutions for problems like this, this is the tutorial for you.
This tutorial will teach you about how to insert multiple images from a folder into your excel.
Steps:
- Open a folder(which contains images) using "Scripting.FileSystemObject".
- Get the files in it.
- Make a loop for all the files
- Check If files name contains "jpg", "jpeg", "gif" (You can add more),
- Activate and resize the cell where you want to insert your image.
- call Insert function by providing the complete path and cell number.
- In Insert function, call ActiveSheet.Pictures.insert(piture path).
- resize the image and set it to the specified cell.
Open a folder(which contains images) using " Scripting.FileSystemObject".
Folderpath = "C:\Users\Sumit Jain\Pictures" Set fso = CreateObject("Scripting.FileSystemObject") NoOfFiles = fso.GetFolder(Folderpath).Files.Count Get the files in it. Set listfiles = fso.GetFolder(Folderpath).Files
Make a loop for all the files
For Each fls In listfiles
Check If files name contains "jpg", "jpeg", "gif" (You can add more),
If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
Activate and resize the cell where you want to insert your image.
Sheets("Object").Range("A" & counter).Value = fls.Name Sheets("Object").Range("B" & counter).ColumnWidth = 25 Sheets("Object").Range("B" & counter).RowHeight = 100 Sheets("Object").Range("B" & counter).Activate
Call Insert function by providing the complete path and cell number.
strCompFilePath = Folderpath & "\" & Trim(fls.Name) Call insert(strCompFilePath, counter)
In Insert function, call ActiveSheet.Pictures.insert(piture path).
ActiveSheet.Pictures.insert(PicPath)
resize the image and set it to the specified cell.
With .ShapeRange .LockAspectRatio = msoTrue .Width = 50 .Height = 70 End With .Left = ActiveSheet.Range("B" & counter).Left .Top = ActiveSheet.Range("B" & counter).Top .Placement = 1 .PrintObject = True
Complete Code:
Also Read:
- FileSystemObject : GetDriveName Method
- VBA-Excel: Maximize, Minimize and Restore Internet Explorer (IE) using Microsoft Excel.
- FileSystemObject : GetParentFolderName Method
- VBA-Excel: Format already written text in a word document – Format All Content
- VBA-Excel: Enumerate all the opened word document