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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Jon Peltier

MrExcel MVP
Joined
May 14, 2003
Messages
5,142
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,641
Messages
5,832,829
Members
430,169
Latest member
sartemiou

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
Top