Inserting pictures from folder automatically with VBA

kefier

New Member
Joined
May 17, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello, in reference to the code from user @yky (Using VBA to pull images from folder to sheet) im using the following the code for inserting pictures automatically from a folder into a worksheet:

VBA Code:
Sub insert_pictures()
    
    Const factor = 0.9  'picture is 90% of the size of cell

    'Variable Declaration
    Dim fsoLibrary As FileSystemObject
    Dim fsoFolder As Object
    Dim sFolderPath As String
    Dim sFileName As Object
    Dim p As Object

    Dim i As Long   'counter
    Dim last_row As Long
    Dim ws As Worksheet

        sFolderPath = "C:\Users\me\Desktop"  'may need to change this line to suit your situation

    
    'Set all the references to the FSO Library
    Set fsoLibrary = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = fsoLibrary.GetFolder(sFolderPath)
    Set ws = ThisWorkbook.Sheets("Sheet1")
    On Error Resume Next
    
    With ws
        .Range("A1") = "Name"
        .Range("B1") = "Picture"
    
        'Loop through each file in a folder
        i = 2
        For Each sFileName In fsoFolder.Files
            .Cells(i, 1) = Left(sFileName.Name, InStr(sFileName.Name, ".") - 1)
            i = i + 1
            '        Debug.Print sFileName.Name
        Next sFileName
        
        last_row = i
        
        Range(.Cells(2, 1), .Cells(i, 1)).Sort key1:=.Cells(2, 1), order1:=xlDescending
    
        For i = 2 To last_row Step 1
    
            Set p = .Shapes.AddPicture(Filename:=sFolderPath _
                & Cells(i, 1).Value & ".jpg", LinkToFile:=False, SaveWithDocument:=True, _
                Left:=.Cells(i, 2).Left, Top:=Cells(i, 2).Top, Width:=-1, Height:=-1)

            p.Width = .Cells(i, 2).Width * factor
            'adjust row height
            If .Cells(i, 2).RowHeight < p.Height / factor Then
                .Cells(i, 2).RowHeight = p.Height / factor
            End If

            p.Left = .Cells(i, 2).Left + (.Cells(i, 2).Width - p.Width) / 2
            p.Top = .Cells(i, 2).Top + (.Cells(i, 2).Height - p.Height) / 2
            Next i
        End With
    
    'Release the memory
    Set fsoLibrary = Nothing
    Set fsoFolder = Nothing

End Sub

My problem is that i am getting the error message "An error occured while importing this file: C:\Users\me\Desktop". I changed the folder name to my specific picture location and figured out that changing the .Addpictures parameter SaveWithDocument:=False to SaveWithDocument:=True makes the error disappear, but then also nothing happens. I'm not an deep expert in VBA and maybe someone on this forum can quickly overlook and solve this situation. I would really appreciate your help :). Thank you!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,

I am sorry i made a mistake describing my problem. I meant the error disappears when im changing the parameter to SaveWithDocument:=False.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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