application.filesearch replacement for 2007

jkomorowski

New Member
Joined
Jun 26, 2008
Messages
12
Hi, I am fairly new to macro writing and am having trouble converting an old macro that I had working for 2003, but that wont work for 2007. I have searched the internet and found numerous other postings on replacing application.filesearch, but I can't seem to get any of them working for me.

I am trying to search a folder that is selected by the user for all .jpg files, and then place all those pictures onto a certain spreadsheet within the excel file. It also formats the pictures. Following is the code:

Code:
   Private Sub BatchProcessThumb2x3()
    Msg = "Select a file containing the photos you want to insert."
    Directory = GetDirectory(Msg)
    If Directory = "" Then Exit Sub
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
 
    On Error Resume Next
    With Application.FileSearch
        .NewSearch
        .LookIn = Directory
        .Filename = "*.jpg"
        .SearchSubFolders = False
        .Execute
 
'   Select begining range
    Worksheets("Thumbnail (2x3)").Select
    Range("B4").Select
'   Loop through all files and process them
 
        For i = 1 To .FoundFiles.Count
            Application.StatusBar = "Processing " & .FoundFiles(i)
'   Import and insert the photo
            ActiveSheet.Pictures.Insert(.FoundFiles(i)).Select
            Selection.ShapeRange.LockAspectRatio = msoTrue
            Selection.ShapeRange.Width = 225#
 
            If Int(i / 2) - (i / 2) <> 0 Then _
                ColOff = 3 Else ColOff = -3
            If Int(i / 2) - (i / 2) <> 0 Then _
                RowOff = 0 Else RowOff = 4
            ActiveCell.Offset(RowOff, ColOff).Select
        Next i
            Application.StatusBar = False
    End With
End Sub


Any and all help would be greatly appreciated.

Thanks, Jason
 
Not a problem, I will figure it out. Thanks again for all of your help. You are awesome!!!!!!!!! You saved me big time!
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Does anyone else know why this may be happening? When I step through it, the correct cell is active, but then when it places the picture it places it in the original cell B4. Here is the code that I have working thus far.

Code:
 Private Sub BatchProcessThumb2x3()
    Msg = "Select a file containing the photos you want to insert."
    Directory = GetDirectory(Msg)
    If Directory = "" Then Exit Sub
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
 
    
    fn = Dir(Directory & "\*jpg")
    Range("B4").Select
    Do While fn <> ""
        i = i + 1
        Application.StatusBar = "Processing " & fn
        ActiveSheet.Pictures.Insert(Directory & "\" & fn).Select
        Selection.ShapeRange.LockAspectRatio = msoTrue
        Selection.ShapeRange.Width = 225#
            If Int(i / 2) - (i / 2) <> 0 Then _
                ColOff = 3 Else ColOff = -3
            If Int(i / 2) - (i / 2) <> 0 Then _
                RowOff = 0 Else RowOff = 4
            ActiveCell.Offset(RowOff, ColOff).Select
            fn = Dir()
    Loop
            Application.StatusBar = False
End Sub

Thanks,
Jason
 
Upvote 0
Just try and see how it goes.
Rich (BB code):
 Private Sub BatchProcessThumb2x3()
    Msg = "Select a file containing the photos you want to insert."
    Directory = GetDirectory(Msg)
    If Directory = "" Then Exit Sub
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
 
    
    fn = Dir(Directory & "\*jpg")
    Set myRng = Range("B4")
    Do While fn <> ""
        i = i + 1
        Application.StatusBar = "Processing " & fn
        myRng.Select
        ActiveSheet.Pictures.Insert(Directory & "\" & fn).Select
        Selection.ShapeRange.LockAspectRatio = msoTrue
        Selection.ShapeRange.Width = 225#
            If i Mod 2 <> 0 Then
                ColOff = 3 : RowOff = 0
            Else 
                ColOff = -3 : RowOff = 4
            End If
            Set myRng = myRng.Offset(RowOff, ColOff)
            fn = Dir()
    Loop
            Application.StatusBar = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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