Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search for files 2

Status
Not open for further replies.

nesplb

Programmer
Jul 29, 2003
109
NO
Hi!
I'm makinge an autoexec sub that is supposed to search through a given folder for a .doc file, open the file and run different macros based on the document's filename. for example, a file with the name "001_test.doc" run one macro while a file with name "002_test.doc" run an other macro. There will only be zero or one file in the folder at any given time.

This is perhaps bad explained, but I hope youunderstand!;)

In advance thanx!

Pål Nesteby

PDC-Tangen
Norway
 
give this a try
Code:
Sub Macro9()
Dim strFileSearch() As Variant
Dim intFileCount As Integer
Set fs = Application.FileSearch
    With fs
    .LookIn = "C:\MY Documnets" ' StrDirectory_To_Search_For_files
    .filename = "*.doc" 'type of file to search for
    .SearchSubFolders = True
        If .Execute > 0 Then
        
            intFileCount = .FoundFiles.Count
            ReDim strFileSearch(intFileCount)
            
            For i = 1 To .FoundFiles.Count
            Debug.Print .FoundFiles.Count
             strFileSearch(i) = .FoundFiles(i)
            Next i
        Else
            MsgBox "There were no files found", vbInformation + vbOKOnly
        End If
    End With
    
    For i = 1 To intFileCount
        Application.Run strFileSearch(i) & "!NameOfMacroTOCall"
    Next i
    

End Sub

Simon
 
Thank you very much!
Exactly what I needed!


Pål Nesteby

PDC-Tangen
Norway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top