Search keyword in folder and return if match found.

pawanhsharma

Board Regular
Joined
Feb 12, 2013
Messages
64
Hi, below is the scenario I am trying to get an answer.
I have a folder with thousands of pdf file. The file name is not standard. I need to search the correct for for my records. As of now this need to be done manually however I am sure there must be some way.

For ex if I put a keyword bga63948, I only need which is the correct file so I open and work on it.

I have found below code but looks like it is for workbooks and not pdf.

Option Explicit
Sub BatchProcessFolderAndSubFolders()
Dim vFolders As Variant
Dim lngIndex As Long
Dim strPath As String
Dim strfilename As String
vFolders = fcnGetSubfolders("d:\My Documents\Word\Word Documents\Word Tips\Macros")
For lngIndex = LBound(vFolders) To UBound(vFolders)
strPath = vFolders(lngIndex)
On Error GoTo Err_NoFiles
strfilename = Dir$(strPath & "*.do*")
While Len(strfilename) <> 0
'You could open the file here and pass it to a function to do something to it.
Debug.Print strfilename
strfilename = Dir$()
Wend
ReEntry:
Next
lbl_Exit:
Exit Sub
Err_NoFiles:
Resume ReEntry
End Sub
Public Function fcnGetSubfolders(ByVal FolderToRead As String) As Variant
Dim AllSubFolders(0) As Variant
On Error Resume Next
System.Cursor = wdCursorWait
If (Right$(FolderToRead, 1) <> "\") Then FolderToRead = FolderToRead & "\"
'Set the path as the first entry in the array and pass the array to the main function.
AllSubFolders(0) = FolderToRead
fcnGetSubfolders = fcnGetAllSubfolders(AllSubFolders)
System.Cursor = wdCursorNormal
'StatusBar = ""
On Error GoTo 0
lbl_Exit:
Exit Function
End Function
Private Function fcnGetAllSubfolders(ByVal AllSubFoldersArray As Variant) As Variant
'This is a recursive function, that is it calls itself as required.
Dim lngCounter As Long
Dim strCurrentFolderName As String
Dim strSubFolderName As String
Dim arrSubFolderList() As String
On Error Resume Next
strCurrentFolderName = CStr(AllSubFoldersArray(UBound(AllSubFoldersArray)))
ReDim arrSubFolderList(0)
strSubFolderName = Dir$(strCurrentFolderName, vbDirectory)
Do While Len(strSubFolderName) <> 0
If strSubFolderName <> "." _
And strSubFolderName <> ".." _
And InStr(1, strSubFolderName, "?") = 0 Then
If (GetAttr(strCurrentFolderName & strSubFolderName) And vbDirectory) = vbDirectory Then
ReDim Preserve arrSubFolderList(UBound(arrSubFolderList) + 1)
arrSubFolderList(UBound(arrSubFolderList)) = strSubFolderName
'StatusBar = "Reading Subfolders... (" & strCurrentFolderName & ": -> " & strSubFolderName & ")"
End If
End If
strSubFolderName = Dir$()
Loop
'Sort the list with the subfolders.
If UBound(arrSubFolderList) > 0 Then WordBasic.SortArray arrSubFolderList()
For lngCounter = 1 To UBound(arrSubFolderList)
'Up the size of the AllSubFoldersArray array by one
ReDim Preserve AllSubFoldersArray(UBound(AllSubFoldersArray) + 1)
AllSubFoldersArray(UBound(AllSubFoldersArray)) = _
strCurrentFolderName & arrSubFolderList(lngCounter) & "\"
AllSubFoldersArray = fcnGetAllSubfolders(AllSubFoldersArray)
Next lngCounter
fcnGetAllSubfolders = AllSubFoldersArray
On Error GoTo 0
End Function

Any help would be greatly appreciated. I only want to find the file. The folder will remain same.

Thanks in advance.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

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