importing photos using macro

lintruthy

Board Regular
Joined
Dec 21, 2016
Messages
54
am working on 100 workbooks, how can I import a different photo per workbook using macro. I have named the photos according to the workbook titles already . please help
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
am working on 100 workbooks, how can I import a different photo per workbook using macro. I have named the photos according to the workbook titles already . please help

lintruthy,
In a new workbook, maybe named 'TEST2', the code could create a list of picture file names including the complete path, then open your Excel workbooks one at a time and insert the picture with the same name from that list, save the workbook, then move to the next workbook.

Typically photos (pictures) are jpg file type. So to compare the Excel workbook name with the photo file name we have to remove the file types and compare just the names. I used three formulae in helper cells to accomplish this on Sheet1 of workbook TEST2:

cell I1 =TRIM(RIGHT(SUBSTITUTE(A1,"",REPT(" ",50)),50)) 'To get just the filename without the path
'The code will copy this formula to each file listed.

cell N1 =LEFT(L1, FIND(".", L1) - 1) 'Removes the file extension '.jpg' or whatever

cell P1 = LEFT(O1, FIND(".", O1) - 1) 'Removes the file extension '.xlsm, .xlsx, or .xls'

Copy the code below into a standard module of workbook TEST2 using Alt+F11, then close and save the workbook as macro enabled. The code assumes your Excel workbooks are all in one folder, and all the photos are in another. Try on a sample copy of your workbooks in a different folder to avoid losing any data.
Good luck!
Perpa
Code:
Sub AddOlEObject()
    Dim mainWorkBook As Workbook
    Dim counter, rw, LR As Long
    Dim nm1, nm2 As String
    
    Application.ScreenUpdating = False
    Set mainWorkBook = ActiveWorkbook
    Sheets("Sheet1").Activate    'Puts all the picture filenames on "Sheet1" of the open workbook; change to the sheet name to suit 
            'NOTE: Be sure to change 'Sheet1' in any line of code below if you change it here.
   
    'Enter the folderpath to wherever your pictures are coming from
    Folderpath = InputBox("Enter the complete folder path to your files" & Chr(13) & " in this format: 'C:\Users\yourfoldername'")
    Set fso = CreateObject("Scripting.FileSystemObject")
    NoOfFiles = fso.GetFolder(Folderpath).Files.Count
    Set listfiles = fso.GetFolder(Folderpath).Files
   'Creates a list of complete path and filenames of all pictures in the directory you entered
    rw = 1
    For Each fls In listfiles
        With Sheets("Sheet1")
            .Range("A" & rw).Value = fls
            rw = rw + 1
            .Range("I" & rw - 1).Copy
            .Range("I" & rw).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 'copies the formula in I1
        End With
    Next fls
    Sheets("Sheet1").Range("I" & rw + 1).Select
    
'Now we need to open each Excel workbook, insert the photo, save and close... and repeat for each workbook in the directory.
LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
myPath = "C:\completePath and Filename\"     'Enter the path to your Excel workbooks that are to receive the pictures
MyName = Dir(myPath, vbDirectory)    'Retrieve the first entry.
Do While MyName <> ""    ' Start the loop.
' Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is not a directory
    If (GetAttr(myPath & MyName) And vbDirectory) <> vbDirectory Then
    
'Find .jpg and .xlsm with same name
       For rw = 1 To LR
         Workbooks("TEST2").Sheets("Sheet1").Range("L1").Value = MyName
         nm1 = Workbooks("TEST2").Sheets("Sheet1").Range("N1").Value    'xlsm name
         Workbooks("TEST2").Sheets("Sheet1").Range("O1").Value = Workbooks("TEST2").Sheets("Sheet1").Range("I" & rw).Value
         nm2 = Workbooks("TEST2").Sheets("Sheet1").Range("P1").Value    'jpg name
            If nm1 = nm2 Then
                Workbooks.Open myPath & MyName
                Call insert1(Workbooks("TEST2").Sheets("Sheet1").Range("A" & rw).Value, rw)
                ActiveWorkbook.Close True
            End If
       Next rw
    End If
    End If
    MyName = Dir    ' Get next entry.
Loop
Workbooks("TEST2").Activate
Application.ScreenUpdating = True
MsgBox "Pictures Copied to Files"
End Sub

Function insert1(PicPath, counter1)
    With ActiveSheet.Pictures.Insert(PicPath)
        With .ShapeRange
            .LockAspectRatio = msoTrue
            '.Width = 50      'Adjust to change the WIDTH of your pictures_Commented out to maintain aspect ratio of pictures
            .Height = 300    'Adjust to change the HEIGHT of your pictures
        End With
        .Left = ActiveSheet.Range("A1").Left
        .Top = ActiveSheet.Range("A1").Top
        .Placement = 1
        .PrintObject = True
    End With
End Function
 
Upvote 0
displays that photos copied but when I check the workbooks, the photos are not there

lintruthy,
From what you describe the code has run because you are getting the message at the end thereof. Therefore
I suspect the filenames of the Excel files do not match the names of the pictures.
Perhaps there are some extra spaces in one or both of the filenames. You can try the TRIM function to remove extra spaces in the formulas:
cell N1 = TRIM(LEFT(L1, FIND(".", L1) - 1)) 'Removes the file extension '.jpg' or whatever
and...
cell P1 = TRIM(LEFT(O1, FIND(".", O1) - 1)) 'Removes the file extension '.xlsm, .xlsx, or .xls'

If that does not help then you need to answer the following:
What format are the pictures you are copying, jpg or what?
Can you provide a sample of the workbook file names and the corresponding picture names with their file extensions?
The complete file path and picture names should be listed on sheet1 of the new workbook Test2.xlsm.
Perpa
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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