ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   using application.filesearch for searching text in PDF (https://www.excelbanter.com/excel-programming/411444-using-application-filesearch-searching-text-pdf.html)

jack

using application.filesearch for searching text in PDF
 
I would use application.filesearch in a excel macro
for searching the body text of PDF files, it works with office files but not
with pdf.

Any possible solution or alternatives to filesearch?
Below my code

Private Sub CommandButton1_Click()
Dim files As Variant
Dim name As Variant
Set fs = CreateObject("Scripting.FileSystemObject")
Cells.ClearContents
With Application.FileSearch
.NewSearch
.LookIn = (UserForm1.TextBox1)
.SearchSubFolders = True
.TextOrProperty = (UserForm1.TextBox2)
.FileType = msoFileTypeAllFiles
If .Execute() 0 Then
Cells(1, 1) = "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
files = .FoundFiles(i)
name = fs.getfilename(files)
Cells(i + 2, 1).Hyperlinks.Add Anchor:=Cells(i + 2, 1),
Address:=files, TextToDisplay:=name
Next i
Else
Cells(1, 1) = "There were no files found."
End If
End With

Thank you in advance


joel

using application.filesearch for searching text in PDF
 
Try this code

Sub AddLinks()
Dim MyFileName As String
Dim strFolder As String
Dim fso As Object
Dim folder As Variant
Dim RowCount As Long

'file(s) to search for

MyFileName = UserForm1.TextBox2
'directory to start searching
strFolder = UserForm1.TextBox1
RowCount = 3
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(strFolder)

Call GetWorksheetsSubFolder(strFolder + "\", MyFileName, RowCount)


End Sub

Sub GetWorksheetsSubFolder(strFolder As String, MyFileName As String,
RowCount As Long)
Dim fso As Object
Dim folder As Variant
Dim BaseName As String
Dim FName As String

Set fso = CreateObject _
("Scripting.FileSystemObject")

Set folder = _
fso.GetFolder(strFolder)

If folder.subfolders.Count 0 Then
For Each sf In folder.subfolders
On Error GoTo 100
Call GetWorksheetsSubFolder(strFolder + sf.name + "\",
MyFileName, RowCount)
100 Next sf
End If
'folder size in bytes
On Error GoTo 200

FName = Dir(strFolder & MyFileName)
Do While FName < ""
BaseName = fso.GetBaseName(FName)
Cells.Hyperlinks.Add _
Anchor:=Range("A" & RowCount), _
Address:=strFolder & FName, _
TextToDisplay:=BaseName
RowCount = RowCount + 1
FName = Dir()
Loop

200 On Error GoTo 0

End Sub


"Jack" wrote:

I would use application.filesearch in a excel macro
for searching the body text of PDF files, it works with office files but not
with pdf.

Any possible solution or alternatives to filesearch?
Below my code

Private Sub CommandButton1_Click()
Dim files As Variant
Dim name As Variant
Set fs = CreateObject("Scripting.FileSystemObject")
Cells.ClearContents
With Application.FileSearch
.NewSearch
.LookIn = (UserForm1.TextBox1)
.SearchSubFolders = True
.TextOrProperty = (UserForm1.TextBox2)
.FileType = msoFileTypeAllFiles
If .Execute() 0 Then
Cells(1, 1) = "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
files = .FoundFiles(i)
name = fs.getfilename(files)
Cells(i + 2, 1).Hyperlinks.Add Anchor:=Cells(i + 2, 1),
Address:=files, TextToDisplay:=name
Next i
Else
Cells(1, 1) = "There were no files found."
End If
End With

Thank you in advance



All times are GMT +1. The time now is 06:42 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com