473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Searching Main Folder and ALL Sub-Folders for File

Hello,

We have a vendor that will supply us many files, and unfortunately will place them in different folders with no uniformity (within Windows Explorer).

I'd like to have the capability to find and open the file based on name, whether it's in a main folder, or one of the subfolders. I could use a wildcard based on the file I'm trying to open.

I.e. - File named "*Net Rebate*" will be in and of the following folders:

Main Folder
Subfolder 1
Subfolder 1a
Subfolder 1b
Subfolder 2
Subfolder 2a
Subfolder 2b

ect..... could be many subfolders.

How can I "Loop" through the main folder and all associated sub-folders to find a file "*Net Rebate*" and open it up?

So far I have:

On Error Resume Next
Dim i As Integer
With Application.FileSearch
.LookIn = ([RebDetail])
'* represents wildcard characters
.Filename = "*Net Rebate*.xls"
If .Execute > 0 Then 'Workbook exists
For i = 1 To .FoundFiles.Count
Workbooks.Open (.FoundFiles(i))
Next i
End If
End With

Any help would be appreciated!!!

Thank you.
Aug 5 '08 #1
6 2459
Option Compare Text 'REQUIRED

Sub OpenSpecificWorkbook()
'Ubentook - August 2008
On Error GoTo ThatHurt
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strPath As String
Dim blnFlag As Boolean

Application.ScreenUpdating = False
' Specify the folder...
strPath = "C:\Documents and Settings\user\My Documents\" 'CHANGE

' Use Microsoft Scripting runtime.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)

' Check for file in the folder and open file.
For Each objFile In objFolder.Files
If objFile.Name Like "*Net Rebate*" Then 'CHANGE
blnFlag = True
Workbooks.Open objFile
Exit For
End If
Next 'objFile
If Not blnFlag Then Call FindFileInSubfolder(objFolder, blnFlag)
CloseOut:
On Error Resume Next
Application.StatusBar = False
Application.ScreenUpdating = True
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Exit Sub
ThatHurt:
Beep
MsgBox "Error " & Err.Number & " " & Err.Description, , _
"PrintWorkbooks"
GoTo CloseOut
End Sub

Function FindFileInSubfolder(ByRef oParentFolder As Object, ByRef bflag As Boolean)
Dim oSubFolder As Object
Dim oFile As Object
For Each oSubFolder In oParentFolder.SubFolders
For Each oFile In oSubFolder.Files
If oFile.Name Like "*Net Rebate*" Then 'CHANGE
Workbooks.Open oFile
bflag = True
Exit For
End If
Next
If Not bflag Then
Call FindFileInSubfolder(oSubFolder, bflag)
Else
Exit For
End If
Next 'oSubFolder
End Function
Aug 5 '08 #2
Thanks SO much!!!
I cannot even say I was even close... so thanks, it saved me time!
Aug 6 '08 #3
You are welcome.
FYI...
"Application.FileSearch" has occasional problems and was removed from xl2007.
I would never rely on it.

The "Dir" function can also be used.
However, its help file could be a little clearer.

I prefer the FileSystemObject as it pretty much uses plain english for its objects, properties and methods.




Thanks SO much!!!
I cannot even say I was even close... so thanks, it saved me time!
Aug 6 '08 #4
Quick follow-up question....

It appears that when the file is not in any folder (main or sub folders) the macro will go right through the code without generating an error.

I tried removing the "On error resume next", however still not generating an error.

How can I implement the code to give an error such as "File does not exist" if there is no folder with the respective name?

Thanks again!
Aug 7 '08 #5
You should only use "On Error Resume Next" to check specific lines of code for errors. Do not use it as a general cure all for an entire procedure.

You are looking for a message not an error.
If the blnFlag variable is false then no file was found.
This code line at the end of the sub should work...
If Not blnFlag Then MsgBox "No file found"


Quick follow-up question....
It appears that when the file is not in any folder (main or sub folders) the macro will go right through the code without generating an error.

I tried removing the "On error resume next", however still not generating an error.

How can I implement the code to give an error such as "File does not exist" if there is no folder with the respective name?

Thanks again!
Aug 7 '08 #6
Thanks again.... your help is appreciated!
Aug 11 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mathew Hill | last post by:
I am a beginner to the more technical aspects of Microsoft Access (2000) and was wondering if any one can help. I have 3 buttons on a form which add, delete and search for a record. However, when I...
8
by: David | last post by:
Hi, I have a form as follows: Main Form (To Select Customer) Sub Form 1 (To Select PO Number, by clicking on field) - Continuous form Sub Form 2 (To view order details)- Continuous form...
2
by: jty202 | last post by:
I use a the function Instr() in VB6 to search for a search-string in String var. It returns the position of the found search-string. Is there such a funtion in ASP.net?
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
3
by: JoelH | last post by:
This is really weird... I'm using Visual Basic 2005 Express I have a form - Form1 - with a control called txtStatusBox I have a module attached to the solution for storing some other code. This...
11
by: TheDataGuy | last post by:
Using MS ACCESS 2002 I developed a FORM, and within that FORM, I created and added a SEARCH BUTTON, and then I created a SUB-FORM, so that when an the END USER clicks on the FORM it will POP-UP...
1
Corster
by: Corster | last post by:
I went through a great deal of hassle to figure this out for myself, but now it is complete, I would like to share it with the world! I know afew other people have had trouble with FindFirst and...
20
by: tkip | last post by:
Hello everyone. I have been working on this DB for quite some time and still can't figure out a way to do what I want to do. It's a database that keep track of drawings and engineering change etc....
1
by: icsnetuk | last post by:
Hi, I have a table called applicants There is a form also called applicants which I use to view / edit the data. On my main dashboard form which is where all of the main options are for the...
1
by: alamodgal | last post by:
hiiiiiii I have a problem in highlighting searching keyword.Actually im using this function for searching Public Function HighLight(ByVal Keyword As String, ByVal ContentFor As String) Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.