Excel 2007 Blues Please help

crenaud

New Member
Joined
May 19, 2007
Messages
3
I'm sort of new to Excel VBA and need some help finding a work around for Application.FileSearch which is no longer supported. The idea is to fill an array with found jpeg file names for use on a form. I use the code below.

Set fs = Application.FileSearch
With fs
.LookIn = modelfolder
.filename = "*.JPG"
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
ReDim pics(.FoundFiles.Count)
NOPIC = .FoundFiles.Count

For i = 1 To .FoundFiles.Count
filename = .FoundFiles(i)
pics(i) = Mid(filename, InStrRev(filename, "\") + 1)
Next i

End If
End With

Could someone help with a solution for this in Excel 2007?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
The function uses Dir to return an array of filenames.

Code:
Function GetFilesFromDirectory(sDirectory As String, sFilter As String) As String()
  Dim sFileName() As String
  Dim sTemp As String
  Dim i As Long
  i = 0
  
  sTemp = Dir(sDirectory & "\" & sFilter)
  
  Do While Len(sTemp) > 0
    i = i + 1
    ReDim Preserve sFileName(1 To i)
    sFileName(i) = sTemp
    sTemp = Dir()
  Loop
  
  GetFilesFromDirectory = sFileName
End Function

In your example, you call it using this:

Code:
Dim sFileNames() as String
sFileNames = GetFilesFromDirectory(modelfolder, "*.jpg")

Use sFileNames in place of .FoundFiles(i)
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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