Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Application.FileSearch is not working??

Folks,

The routine below loads all the file names in the "DataDirectory" into a
ComboBox where the first 4 letters of the file name are the same as the
"SerialNumber". It works perfectly on some XP SP2 machines, but leaves out
files reciently added to the "DataDirectory" on other XP SP2 machines?
Always works on mapped drives, but has problems on local drives. Restart or
running Explore and hitting F5 a few times seems to solve the problem.

It looks like XP will buffer directories, but will not always update them
properly. Is something wrong with my code or is the Application.FileSearch
method just flakey? Is there a way to get around this problem?

I really need this code, or something that accomplishes the same thing, to
work Any Ideas?

Thanks,

Allan P. London, CPA
San Francisco, CA

__________________________________________________ ____________
Private Sub UserForm_Initialize()

'Load scenario file names into ComboBox1



Dim fs As FileSearch

Dim I As Integer



On Error GoTo ErrorCode

Set fs = Application.FileSearch



With fs

.RefreshScopes

.NewSearch

.LookIn = DataDirectory

.Filename = Mid(Range("theSerialNumber"), 1, 4) & "*.xls"

If .Execute(SortBy:=msoSortByFileName, _

SortOrder:=msoSortOrderAscending) 0 Then

'MsgBox .FoundFiles.Count & " file(s) found."

For I = 1 To .FoundFiles.count

ComboBox1.AddItem UCase(.FoundFiles(I))

Next I

Else

MsgBox "There were no files found for " & CompanyName & _

" on the " & DataDirectory & "Directory"

GoTo ErrorCode

End If

End With

Exit Sub



ErrorCode:

Retval = False

Unload RecallScenario

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Application.FileSearch is not working??

Hi Alondon,

There have been munerous posts suggesting that Filesearch , at least in its
xl2002 implementation is very flakey an I am aware that many have dropped it
from use.

See for example:

http://tinyurl.com/6p6vl


and

http://tinyurl.com/6cen2



---
Regards,
Norman



"alondon" wrote in message
...
Folks,

The routine below loads all the file names in the "DataDirectory" into a
ComboBox where the first 4 letters of the file name are the same as the
"SerialNumber". It works perfectly on some XP SP2 machines, but leaves
out files reciently added to the "DataDirectory" on other XP SP2 machines?
Always works on mapped drives, but has problems on local drives. Restart
or running Explore and hitting F5 a few times seems to solve the problem.

It looks like XP will buffer directories, but will not always update them
properly. Is something wrong with my code or is the
Application.FileSearch method just flakey? Is there a way to get around
this problem?

I really need this code, or something that accomplishes the same thing, to
work Any Ideas?

Thanks,

Allan P. London, CPA
San Francisco, CA

__________________________________________________ ____________
Private Sub UserForm_Initialize()

'Load scenario file names into ComboBox1



Dim fs As FileSearch

Dim I As Integer



On Error GoTo ErrorCode

Set fs = Application.FileSearch



With fs

.RefreshScopes

.NewSearch

.LookIn = DataDirectory

.Filename = Mid(Range("theSerialNumber"), 1, 4) & "*.xls"

If .Execute(SortBy:=msoSortByFileName, _

SortOrder:=msoSortOrderAscending) 0 Then

'MsgBox .FoundFiles.Count & " file(s) found."

For I = 1 To .FoundFiles.count

ComboBox1.AddItem UCase(.FoundFiles(I))

Next I

Else

MsgBox "There were no files found for " & CompanyName & _

" on the " & DataDirectory & "Directory"

GoTo ErrorCode

End If

End With

Exit Sub



ErrorCode:

Retval = False

Unload RecallScenario

End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Application.FileSearch is not working??

Hi Alondon,

Looking at your code, try changing the line:

.Filename = Mid(Range("theSerialNumber"), 1, 4) & "*.xls"



to:

.Filename = Mid(Range("theSerialNumber"), 1, 4) & ".xls"


---
Regards,
Norman



"alondon" wrote in message
...
Folks,

The routine below loads all the file names in the "DataDirectory" into a
ComboBox where the first 4 letters of the file name are the same as the
"SerialNumber". It works perfectly on some XP SP2 machines, but leaves
out files reciently added to the "DataDirectory" on other XP SP2 machines?
Always works on mapped drives, but has problems on local drives. Restart
or running Explore and hitting F5 a few times seems to solve the problem.

It looks like XP will buffer directories, but will not always update them
properly. Is something wrong with my code or is the
Application.FileSearch method just flakey? Is there a way to get around
this problem?

I really need this code, or something that accomplishes the same thing, to
work Any Ideas?

Thanks,

Allan P. London, CPA
San Francisco, CA

__________________________________________________ ____________
Private Sub UserForm_Initialize()

'Load scenario file names into ComboBox1



Dim fs As FileSearch

Dim I As Integer



On Error GoTo ErrorCode

Set fs = Application.FileSearch



With fs

.RefreshScopes

.NewSearch

.LookIn = DataDirectory

.Filename = Mid(Range("theSerialNumber"), 1, 4) & "*.xls"

If .Execute(SortBy:=msoSortByFileName, _

SortOrder:=msoSortOrderAscending) 0 Then

'MsgBox .FoundFiles.Count & " file(s) found."

For I = 1 To .FoundFiles.count

ComboBox1.AddItem UCase(.FoundFiles(I))

Next I

Else

MsgBox "There were no files found for " & CompanyName & _

" on the " & DataDirectory & "Directory"

GoTo ErrorCode

End If

End With

Exit Sub



ErrorCode:

Retval = False

Unload RecallScenario

End Sub




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Application.FileSearch is not working??

You could as easily use th eDIR() function.


DIM fn as string
dim spec as string

spec = LEFT(Range("theSerialNumber").Value, 4) & "*.xls"

fn = DIR(spec)
DO WHILE fn < ""
ComboBox1.AddItem
fn = DIR()
LOOP





"alondon" wrote:

Folks,

The routine below loads all the file names in the "DataDirectory" into a
ComboBox where the first 4 letters of the file name are the same as the
"SerialNumber". It works perfectly on some XP SP2 machines, but leaves out
files reciently added to the "DataDirectory" on other XP SP2 machines?
Always works on mapped drives, but has problems on local drives. Restart or
running Explore and hitting F5 a few times seems to solve the problem.

It looks like XP will buffer directories, but will not always update them
properly. Is something wrong with my code or is the Application.FileSearch
method just flakey? Is there a way to get around this problem?

I really need this code, or something that accomplishes the same thing, to
work Any Ideas?

Thanks,

Allan P. London, CPA
San Francisco, CA

__________________________________________________ ____________
Private Sub UserForm_Initialize()

'Load scenario file names into ComboBox1



Dim fs As FileSearch

Dim I As Integer



On Error GoTo ErrorCode

Set fs = Application.FileSearch



With fs

.RefreshScopes

.NewSearch

.LookIn = DataDirectory

.Filename = Mid(Range("theSerialNumber"), 1, 4) & "*.xls"

If .Execute(SortBy:=msoSortByFileName, _

SortOrder:=msoSortOrderAscending) 0 Then

'MsgBox .FoundFiles.Count & " file(s) found."

For I = 1 To .FoundFiles.count

ComboBox1.AddItem UCase(.FoundFiles(I))

Next I

Else

MsgBox "There were no files found for " & CompanyName & _

" on the " & DataDirectory & "Directory"

GoTo ErrorCode

End If

End With

Exit Sub



ErrorCode:

Retval = False

Unload RecallScenario

End Sub



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Application.FileSearch is not working??

Allan,

As an alternative approach (the other suggestions will probably work)...

FileScriptingObject code is included with all recent versions of Windows.
It can be freely mixed in with VBA code.
It is much more reliable than the FileSearch code in VBA (in my experience).
The following code worked for me. The variables were changed so the code
could be tested on my machine.
With some minor changes it could work for you.

'-----------------------------------------
Sub UserForm_Initialize()
'Requires project reference to "Microsoft Scripting Runtime"
'(scrrun.dll)
'Jim Cone - January 17, 2005
On Error GoTo ErrorCode
Dim objFso As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objFile As Scripting.File
Dim strDataDirectory As String
Dim strSerialNumber As String
Dim blnFound As Boolean

strSerialNumber = "####"
strDataDirectory = "C:\Documents and Settings\user\My Documents\Excel Files"
Set objFso = New Scripting.FileSystemObject
Set objFolder = objFso.GetFolder(strDataDirectory)

For Each objFile In objFolder.Files
If objFile.Name Like strSerialNumber & "*.xls" Then
ComboBox1.AddItem UCase(objFile.Name)
blnFound = True
End If
Next 'objFile

If Not blnFound Then
MsgBox "There were no files found for CompanyName" & vbCr & _
" on the " & strDataDirectory & " Directory"
GoTo ErrorCode
End If
Set objFile = Nothing
Set objFolder = Nothing
Set objFso = Nothing
Exit Sub

ErrorCode:
'do stuff
End Sub
'---------------------------------------------

Regards,
Jim Cone
San Francisco, USA


"alondon" wrote in message
...
Folks,

The routine below loads all the file names in the "DataDirectory" into a
ComboBox where the first 4 letters of the file name are the same as the
"SerialNumber". It works perfectly on some XP SP2 machines, but leaves out
files reciently added to the "DataDirectory" on other XP SP2 machines?
Always works on mapped drives, but has problems on local drives. Restart or
running Explore and hitting F5 a few times seems to solve the problem.
It looks like XP will buffer directories, but will not always update them
properly. Is something wrong with my code or is the Application.FileSearch
method just flakey? Is there a way to get around this problem?
I really need this code, or something that accomplishes the same thing, to
work Any Ideas?
Thanks,
Allan P. London, CPA
San Francisco, CA

__________________________________________________ ______
Private Sub UserForm_Initialize()
'Load scenario file names into ComboBox1
Dim fs As FileSearch
Dim I As Integer
On Error GoTo ErrorCode
Set fs = Application.FileSearch
With fs
.RefreshScopes
.NewSearch
.LookIn = DataDirectory
.Filename = Mid(Range("theSerialNumber"), 1, 4) & "*.xls"
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
'MsgBox .FoundFiles.Count & " file(s) found."
For I = 1 To .FoundFiles.count
ComboBox1.AddItem UCase(.FoundFiles(I))
Next I
Else
MsgBox "There were no files found for " & CompanyName & _
" on the " & DataDirectory & "Directory"
GoTo ErrorCode
End If
End With
Exit Sub
ErrorCode:
Retval = False
Unload RecallScenario
End Sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Application.FileSearch is not working??

Folks,

Thank you all for your help.

Norman convinced me that I needed an alternative to the "flakey" FileSearch
method. I finally settled on Patrick Malloy's solution as the most direct
and simplest (see below). However, I learned a lot by reviewing Jim's
elegant scripting solution; unfortunately my winery solutions go to a lot of
people that would never be able to properly reference a type library.

Wow, you folks always come through so I can continue to promise folks
solutions that I could not possibly deliver without your help:)

Cheers,

Allan P. London, CPA

__________________________________________________ __________
THE SOLUTION:

Private Sub UserForm_Initialize()

'Load scenario file names into ComboBox1

'Complements of Patrick Molloy, Excel Programming User Group



Dim FoundFile As Boolean

Dim fn As String

Dim Spec As String

Dim I As Integer



On Error GoTo ErrorCode



'Initialize

ChDir DataDirectory

FoundFile = False

Spec = Left(Range("theSerialNumber").Value, 4) & "*.xls"

fn = Dir(Spec)



'Load Combo Box

Do While fn < ""

FoundFile = True

ComboBox1.AddItem fn

'Iterate to next qualified file name

fn = Dir()

Loop



'Cleanup

ChDir theSystemLocation

If Not FoundFile Then GoTo ErrorCode



Exit Sub



ErrorCode:

MsgBox "There were no files found for " & CompanyName & _

" on the " & DataDirectory & "Directory"

Retval = False

Unload RecallScenario



End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application.FileSearch Cleberton(Brazilian) Excel Discussion (Misc queries) 2 October 26th 09 01:21 PM
Customising Application.Filesearch to process textpad kind of files Hari[_3_] Excel Programming 6 January 15th 05 06:26 AM
Application.Filesearch yields incorrect results?? Ed[_23_] Excel Programming 2 August 24th 04 08:59 PM
VBA Application.FileSearch Roger Frye Excel Programming 0 March 5th 04 04:07 AM
Application::DisplayAlerts not working Howard Dierking Excel Programming 2 February 11th 04 12:04 PM


All times are GMT +1. The time now is 07:49 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"