Results 1 to 4 of 4

VBA to search string in PDF and extract pages

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    VBA to search string in PDF and extract pages

    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
    Last edited by shiva_reshs; 09-25-2018 at 12:45 AM.
    Keep the Forum clean :


    1. Use [ code ] code tags [ /code ]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. Show appreciation to those who have helped you by clicking * Add Reputation below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to search a range for a string and then extract certain dta from string
    By gratedane8 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-08-2018, 06:09 PM
  2. [SOLVED] Search and Find - To extract some text from a string of text
    By Badvgood in forum Excel General
    Replies: 4
    Last Post: 11-01-2016, 01:04 PM
  3. Word - modify macro to loop search procedure and then extract pages as pdf
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2016, 06:47 AM
  4. [SOLVED] Search for string across header row, then search for another string down found column
    By TucsonJack in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2012, 02:09 PM
  5. Replies: 1
    Last Post: 04-23-2012, 10:27 AM
  6. Replies: 7
    Last Post: 03-29-2012, 01:25 PM
  7. search a string withing a string : find / search hangs
    By itarnak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-24-2005, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1