Hi,
I spent hours on finding way to search a string in PDF and extract the page. THe one which i found around web, is search once or there are some which only search and give you page number. HOwever, my purpose was to extract the page where it is found. After many attempt, below code works. Sharing out here, if anyone want to use.
![]()
Sub test_with_PDF() ' Dim objApp As Object Dim objPDDoc As Object Dim objjso As Object Dim wordsCount As Long Dim page As Long Dim i As Long Dim strData As String Dim strFileName As String strFileName = "C:\Payslips.pdf" ' The path where file is saved Set objApp = CreateObject("AcroExch.App") Set objPDDoc = CreateObject("AcroExch.PDDoc") If objPDDoc.Open(strFileName) Then Set objjso = objPDDoc.GetJSObject For page = 0 To objPDDoc.GetNumPages - 1 wordsCount = objjso.getPageNumWords(page) For i = 0 To wordsCount If objjso.getPageNthWord(page, i) = "Termination" Then MsgBox "I found a Termination on page " & page Folder = "C:\Outpuut" ' Path where out put file need to be saved. PageNos = PageNos + 1 Set newPDF = CreateObject("AcroExch.PDDoc") newPDF.Create NewName = Folder & " Page_" & i & "_of_" & PageNos & ".pdf" newPDF.InsertPages -1, objPDDoc, page, 1, 0 newPDF.Save 1, NewName newPDF.Close Set newPDF = Nothing Exit For End If Next i Next MsgBox "Done" Else MsgBox "error!" End If End Sub
Bookmarks