fiberboysa
Board Regular
- Joined
- Apr 25, 2012
- Messages
- 106
- Office Version
- 365
- Platform
- Windows
I found the following VBA code here which search file word by word and page by page and returns the page number. I wish that it could be modified to search the file page by page NOT word by word and return page number. Then it will be more fast in my opinion.
VBA Code:
Sub FindTextAcroAll() Dim FindWord 'Word you want to search
Dim acroAppObj As Object
Dim PDFDocObj As Object
Dim myPDFPageHiliteObj As Object
Dim Cell As Range, Rng As Range
Dim PDFJScriptObj As Object
Dim wordHilite As Object
Dim Check3 As String
Dim pAcroPDPage As CAcroPDPage
Dim iword As Integer, iTotalWords As Integer
Dim numOfPage As Integer, Nthpage As Integer
Dim word As String, sPath As String
Dim PageNumber As Long
Set acroAppObj = CreateObject("AcroExch.App")
Set PDFDocObj = CreateObject("AcroExch.PDDoc")
Set myPDFPageHiliteObj = CreateObject("AcroExch.HiliteList")
Check3 = myPDFPageHiliteObj.Add(0, 32767)
sPath = Application.GetOpenFilename("PDF Files (*.pdf), *.pdf") 'Path of pdf where you want to search
If sPath = "False" Then GoTo GetOut
' acroAppObj.Show
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set Rng = Selection
'x = 0 uncomment if plan to cascade pages
For Each Cell In Rng
y = 1
FindWord = Cell
PDFDocObj.Open (sPath)
numOfPage = PDFDocObj.GetNumPages
word = vbNullString
Set PDFJScriptObj = Nothing
For Nthpage = 0 To numOfPage - 1 'substitute x for 0 if you want it go back and search past last known word
Set pAcroPDPage = PDFDocObj.AcquirePage(Nthpage)
Set wordHilite = pAcroPDPage.CreateWordHilite(myPDFPageHiliteObj)
Set PDFJScriptObj = PDFDocObj.GetJSObject
iTotalWords = wordHilite.GetNumText
iTotalWords = PDFJScriptObj.getPageNumWords(Nthpage)
''check the each word
For iword = 0 To iTotalWords - 1
word = Trim(CStr(PDFJScriptObj.getPageNthWord(Nthpage, iword)))
If word <> "" Then
If LCase(word) = LCase(FindWord) Then 'LCase to make keyword search non-case specific
x = Nthpage + 1
PageNumber = Nthpage + 1
Cell.Offset(0, y).Value = PageNumber
y = y + 1
GoTo Skip:
End If
word = ""
End If
Next iword
Skip:
Next Nthpage
Next Cell
MsgBox "Done"
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
GetOut:
End Sub