+ Reply to Thread
Results 1 to 3 of 3

Insert newest Picture in folder into Spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-18-2012
    Location
    stoke
    MS-Off Ver
    Excel 2007
    Posts
    3

    Insert newest Picture in folder into Spreadsheet

    Hi,
    I wonder if anyone can help?

    I am trying to figure out how to insert the newest JPG file from a given folder into a certain location in a spreadsheet.

    The pictures are images captured from a camera which are inserted as JPG files into the same folder each time.
    I need to be able to insert the most recent file into the spreadsheet.

    Any help would be appreciated

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Insert newest Picture in folder into Spreadsheet

    123rich456,

    Welcome to the forum!
    Give the below code a try. When you run it, you'll be asked to select the folder containing the JPG files. After that, you'll be asked to select the cell that the picture will be placed in. This is the cell that will have the top-left corner of the image.
    Sub Insert_Newest_JPG()
        
        Dim rngDest As Range
        Dim FSO As Object
        Dim oFile As Object
        Dim oPic As Object
        Dim dTimeNow As Double
        Dim dTimeSince As Double
        Dim strFolderPath As String
        Dim strFilePath As String
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            If .Show = False Then Exit Sub  'Pressed cancel
            strFolderPath = .SelectedItems(1)
        End With
        
        
        dTimeNow = Now
        dTimeSince = 1000000
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
        For Each oFile In FSO.GetFolder(strFolderPath).Files
            If Right(oFile.Name, 4) = ".jpg" Then
                If dTimeNow - oFile.DateLastModified < dTimeSince Then
                    dTimeSince = dTimeNow - oFile.DateLastModified
                    strFilePath = oFile.Path
                End If
            End If
        Next oFile
        
        If Len(strFilePath) > 0 Then
            Set oPic = LoadPicture(strFilePath)
            On Error Resume Next
            Set rngDest = Application.InputBox("Select the estination cell for the image", "Desination Cell", Type:=8)
            On Error GoTo 0
            If rngDest Is Nothing Then Exit Sub 'Pressed cancel
            Set rngDest = rngDest.Cells(1)
            ActiveSheet.Shapes.AddPicture strFilePath, msoFalse, msoTrue, rngDest.Left, rngDest.Top, Round(oPic.Width / 26.458, 0), Round(oPic.Height / 26.458, 0)
        Else
            MsgBox "No .jpg files found in " & strFolderPath, , "Insert Image Error"
        End If
        
    End Sub


    How to use a macro:
    1. Make a copy of the workbook the macro will be run on
      • Always run new code on a workbook copy, just in case the code doesn't run smoothly
      • This is especially true of any code that deletes anything
    2. In the copied workbook, press ALT+F11 to open the Visual Basic Editor
    3. Insert | Module
    4. Copy the provided code and paste into the module
    5. Close the Visual Basic Editor
    6. In Excel, press ALT+F8 to bring up the list of available macros to run
    7. Double-click the desired macro (I named this one Insert_Newest_JPG)
    Last edited by tigeravatar; 06-18-2012 at 02:25 PM.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Insert newest Picture in folder into Spreadsheet

    Alter path to Suit
    Code places the latest JPG on sheet.
    Sub nPic()
       Dim fNam As String
       Dim Pth As String
        Dim fso As Object, f As Object, ff As Object, f1 As Object
        Dim oMax As Date
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.GetFolder("C:\documents and settings\test\Desktop\My Folder\Claydon Map")
        Set ff = f.Files
        Pth = f.Path & "\"
        
        For Each f1 In ff
            If f1.Type = "JPEG Image" Then
                oMax = Application.Max(oMax, FileDateTime(f1))
            If oMax > FileDateTime(f1) Then
                fNam = f1.Name
            End If
    End If
    Next
    
    Pth = Pth & fNam
    Dim MyObj As Picture
    If fNam = "False" Then Exit Sub
    
    Set MyObj = ActiveSheet.Pictures.Insert(Pth)
    
        With MyObj
            With .ShapeRange
    'Alter to suit
    '        .LockAspectRatio = msoFalse
    '        .IncrementRotation 90#
    '        .IncrementRotation 180#
    '        .Height = 350
    '        .Width = 500
            .Top = Range("A1").Top
            .Left = Range("A1").Left
            End With
        .Placement = xlMoveAndSize
        End With
    
    Set MyObj = Nothing
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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