+ Reply to Thread
Results 1 to 13 of 13

How to insert > 137 no. pictures in excel file?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    How to insert > 137 no. pictures in excel file?

    The 150 pictures in F:\PIC have to be inserted in attached excel file.
    But it only works if F:\PIC has 137 pictures;
    If the F:\PIC has more than 137 pictures VBA is not function.
    Please help to revise the VBA.
    (System: Win 10+ Office 365)
    Folder F:\PIC is too large to be uploaded.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: How to insert > 137 no. pictures in excel file?

    Have you considered just having a hyperlink to those pics, instead of putting them in the file?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to insert > 137 no. pictures in excel file?

    The pics cannot be hyperlinked as the F:\PIC will be removed later; hence I tried to copy and paste the pics to remove the link but failure.

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: How to insert > 137 no. pictures in excel file?

    Try this code
    Sub InsertPictures()
        Dim sFile As Variant, r As Range
        Dim sh As Shape, I As Integer
    
        On Error Resume Next
            For I = 1 To 3
                Set r = Cells(I, 1)
                sFile = ThisWorkbook.Path & "\" & I & ".JPG"
                If sFile = False Or Not (Len(Dir(sFile)) > 0) Then GoTo Skipper
        
                Set sh = ActiveSheet.Shapes.AddPicture(Filename:=sFile, LinkToFile:=msoFalse, SaveWithDocument:=msoCTrue, Left:=r.Left, Top:=r.Top, Width:=r.Width, Height:=r.Height)
        
                With sh
                    .Top = r.Top
                    .Left = r.Left
                    .Height = r.RowHeight
                    .Placement = xlMoveAndSize
                End With
    Skipper:
                sFile = ""
            Next I
        On Error GoTo 0
    End Sub
    Rename your pictures 1 - 2 - 3 and so on
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  5. #5
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to insert > 137 no. pictures in excel file?

    How to modify VBA if:
    1. Pictures come from folder F:\PIC;
    2. Picture names are SKM_C454e15101411100-001, SKM_C454e15101411100-002 ... SKM_C454e15101411100-150.

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: How to insert > 137 no. pictures in excel file?

    Try this code
    Sub AddOlEObject()
        Dim folderPath As String, strCompFilePath As String
        Dim FSO, FLS, listFiles
        Dim noOfFiles As Long, Counter As Long
        Dim mainWorkBook As Workbook
        Dim Sh As Worksheet
    
        Set mainWorkBook = ActiveWorkbook
        Set Sh = mainWorkBook.Sheets("Sheet1")
    
        Sh.Activate
        folderPath = "F:\PIC"
        Set FSO = CreateObject("Scripting.FileSystemObject")
        noOfFiles = FSO.GetFolder(folderPath).Files.Count
        Set listFiles = FSO.GetFolder(folderPath).Files
        
        For Each FLS In listFiles
            strCompFilePath = folderPath & "\" & Trim(FLS.Name)
            If strCompFilePath <> "" Then
                If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 _
                    Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 _
                    Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
                    Counter = Counter + 1
                    Sh.Range("A" & Counter).Value = FLS.Name
                    Sh.Range("B" & Counter).Activate
                    Call Insert(strCompFilePath, Counter)
                    Sh.Activate
                End If
            End If
        Next FLS
    End Sub
    
    Function Insert(PicPath, Counter)
        Dim R As Range, Sh As Shape
        
        Set R = ActiveSheet.Range("A" & Counter)
        Set Sh = ActiveSheet.Shapes.AddPicture(Filename:=PicPath, LinkToFile:=msoFalse, SaveWithDocument:=msoCTrue, Left:=R.Left, Top:=R.Top, Width:=R.Width, Height:=R.Height)
        
        With Sh
            .Top = R.Top
            .Left = R.Left
            .Height = R.RowHeight
            .Placement = xlMoveAndSize
        End With
    End Function

  7. #7
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to insert > 137 no. pictures in excel file?

    Thanks YasserKhalil.
    Please revise VBA as follows:

    1. No need to add filename at column A;
    2. Size and location of pictures should match existing print range.
    (Expected output please refer to attached excel)
    3. The size of print range should match with no. of pictures.
    (i.e. print range should be increased for more pictures)
    4. The pictures should be transparent.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: How to insert > 137 no. pictures in excel file?

    ِAs for first point
    Just remove this line
    Sh.Range("A" & Counter).Value = FLS.Name
    As for other points, I can't deal with these points. Let's wait for experts

  9. #9
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to insert > 137 no. pictures in excel file?

    Thanks YasserKhalil.
    I'll solve the remaining points myself.

  10. #10
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to insert > 137 no. pictures in excel file?

    Thanks YasserKhalil.
    I'll solve the remaining points myself.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: How to insert > 137 no. pictures in excel file?

    You're welcome and it is better to share your solution for other points .. as we need to learn new topics
    Best Regards

  12. #12
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to insert > 137 no. pictures in excel file?

    How to revise your VBA if only picture SKM_C454e15101411100-### in F:\Pic are inserted to excel
    and not insert other pictures with different filename in the same folder?

  13. #13
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to insert > 137 no. pictures in excel file?

    Hello YasserKhalil*,

    Further to your reply to my thread "Re: How to insert > 137 no. pictures in excel file?" dated 4 Feb 16 as below,
    the following VBA can apply to all jpeg in folder "F:\PIC".

    Please advise how to revise the VBA so that it can apply to all jpeg to all sub-folder in "F:\PIC".
    (as there are also jpegs in subfolders)





    Sub AddOlEObject()
    Dim folderPath As String, strCompFilePath As String
    Dim FSO, FLS, listFiles
    Dim noOfFiles As Long, Counter As Long
    Dim mainWorkBook As Workbook
    Dim Sh As Worksheet

    Set mainWorkBook = ActiveWorkbook
    Set Sh = mainWorkBook.Sheets("Sheet1")

    Sh.Activate
    folderPath = "F:\PIC"
    Set FSO = CreateObject("Scripting.FileSystemObject")
    noOfFiles = FSO.GetFolder(folderPath).Files.Count
    Set listFiles = FSO.GetFolder(folderPath).Files

    For Each FLS In listFiles
    strCompFilePath = folderPath & "\" & Trim(FLS.Name)
    If strCompFilePath <> "" Then
    If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 _
    Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 _
    Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
    Counter = Counter + 1
    Sh.Range("A" & Counter).Value = FLS.Name
    Sh.Range("B" & Counter).Activate
    Call Insert(strCompFilePath, Counter)
    Sh.Activate
    End If
    End If
    Next FLS
    End Sub

    Function Insert(PicPath, Counter)
    Dim R As Range, Sh As Shape

    Set R = ActiveSheet.Range("A" & Counter)
    Set Sh = ActiveSheet.Shapes.AddPicture(Filename:=PicPath, LinkToFile:=msoFalse, SaveWithDocument:=msoCTrue, Left:=R.Left, Top:=R.Top, Width:=R.Width, Height:=R.Height)

    With Sh
    .Top = R.Top
    .Left = R.Left
    .Height = R.RowHeight
    .Placement = xlMoveAndSize
    End With
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] inserting pictures in excel sheet duplicating on insert.
    By stielo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2015, 10:35 AM
  2. need a vba code to insert pictures with a corresponding file name
    By whan714 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-19-2015, 02:50 PM
  3. Insert Pictures In Excel 2010
    By Hemish in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2014, 11:51 AM
  4. Insert/Delete Multiple Pictures but 1 or more pictures not available
    By MrNoodles in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2012, 02:10 PM
  5. Pictures insert in excel and affected by filter sorting
    By otterandrews in forum Excel General
    Replies: 0
    Last Post: 06-20-2012, 01:54 AM
  6. Replies: 0
    Last Post: 02-12-2006, 02:15 AM
  7. Replies: 7
    Last Post: 11-14-2005, 10:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1