Consulting

Results 1 to 2 of 2

Thread: How to search text in pdf files in one folder

  1. #1
    VBAX Contributor
    Joined
    Nov 2009
    Posts
    114
    Location

    How to search text in pdf files in one folder

    Sub Search_PDFs_For_String()
        
        Dim ws As Worksheet
        Dim AcroPDDoc As Object
        Dim AcroAVDoc As Object
        Dim appObj As Object
        
        Dim FSO As Object, FSOfolder As Object, FSOfile As Object
        Dim searchString As String
        Dim PDF_path As String
        Dim blnSearch As Boolean
        Dim lr As Long, r_output As Long
        
        Set ws = ThisWorkbook.Worksheets("PDF_Search")
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set AcroPDDoc = CreateObject("AcroExch.PDDoc")
        
        'Excel environment - speed things up
    '    With Application
    '        .ScreenUpdating = False
    '        .DisplayAlerts = False
    '        .EnableEvents = False
    '        .Calculation = xlCalculationManual
    '    End With
        
        'Get folder path and search phrase
        With ws
            PDF_path = .Range("A1").Value
            searchString = .Range("A2").Value
            .Range("A4:C4").Value = Split("Path,File,Found?", ",")
            lr = .Cells(.Rows.Count, "A").End(xlUp).Row
            If lr >= 5 Then .Range("A5:C5", .Cells(lr, "A")).ClearContents
            r_output = 5
        End With
        
        'Search each file for the phrase
        Set appObj = CreateObject("AcroExch.App")
         Set FSOfolder = FSO.GetFolder(PDF_path)
         
        For Each FSOfile In FSOfolder.Files
        
       
            
            If LCase(FSOfile.Name) Like "*.pdf" Then
            
                'Open the PDF file and check if the open was successful.
                
                If AcroPDDoc.Open(FSOfile.Path) Then
                
                    Set AcroAVDoc = AcroPDDoc.OpenAVDoc("")
                    blnSearch = AcroAVDoc.findText(szText:=searchString, _
                                                  bCaseSensitive:=False, _
                                                  bWholeWordsOnly:=True, _
                                                  bReset:=2)
                    AcroAVDoc.Close bNoSave:=True
                
                    'log results
                    With ws
                        .Cells(r_output, 1).Value = FSOfile.Path
                        .Cells(r_output, 2).Value = FSOfile.Name
                        .Cells(r_output, 3).Value = blnSearch
                    End With
                    r_output = r_output + 1
                    DoEvents
                    
                End If
            
            End If
            
              Application.Wait (Now + TimeValue("0:00:02"))
    
    
        Next
        
           Set AcroAVDoc = Nothing
        Set AcroPDDoc = Nothing
        appObj.Exit
        'Destroy objects
    
    
        
        'Excel environment - restore
    '    With Application
    '        .ScreenUpdating = True
    '        .DisplayAlerts = True
    '        .EnableEvents = True
    '        .Calculation = xlCalculationAutomatic
    '    End With
    
    
    End Sub
    Why only the first one is okay, another is not working. Thanks!

  2. #2
    VBAX Contributor
    Joined
    Nov 2009
    Posts
    114
    Location
    Quote Originally Posted by clif View Post
    Sub Search_PDFs_For_String()
        
        Dim ws As Worksheet
        Dim AcroPDDoc As Object
        Dim AcroAVDoc As Object
        Dim appObj As Object
        
        Dim FSO As Object, FSOfolder As Object, FSOfile As Object
        Dim searchString As String
        Dim PDF_path As String
        Dim blnSearch As Boolean
        Dim lr As Long, r_output As Long
        
        Set ws = ThisWorkbook.Worksheets("PDF_Search")
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set AcroPDDoc = CreateObject("AcroExch.PDDoc")
        
        'Excel environment - speed things up
    '    With Application
    '        .ScreenUpdating = False
    '        .DisplayAlerts = False
    '        .EnableEvents = False
    '        .Calculation = xlCalculationManual
    '    End With
        
        'Get folder path and search phrase
        With ws
            PDF_path = .Range("A1").Value
            searchString = .Range("A2").Value
            .Range("A4:C4").Value = Split("Path,File,Found?", ",")
            lr = .Cells(.Rows.Count, "A").End(xlUp).Row
            If lr >= 5 Then .Range("A5:C5", .Cells(lr, "A")).ClearContents
            r_output = 5
        End With
        
        'Search each file for the phrase
        Set appObj = CreateObject("AcroExch.App")
         Set FSOfolder = FSO.GetFolder(PDF_path)
         
        For Each FSOfile In FSOfolder.Files
        
       
            
            If LCase(FSOfile.Name) Like "*.pdf" Then
            
                'Open the PDF file and check if the open was successful.
                
                If AcroPDDoc.Open(FSOfile.Path) Then
                
                    Set AcroAVDoc = AcroPDDoc.OpenAVDoc("")
                    blnSearch = AcroAVDoc.findText(szText:=searchString, _
                                                  bCaseSensitive:=False, _
                                                  bWholeWordsOnly:=True, _
                                                  bReset:=2)
                    AcroAVDoc.Close bNoSave:=True
                
                    'log results
                    With ws
                        .Cells(r_output, 1).Value = FSOfile.Path
                        .Cells(r_output, 2).Value = FSOfile.Name
                        .Cells(r_output, 3).Value = blnSearch
                    End With
                    r_output = r_output + 1
                    DoEvents
                    
                End If
            
            End If
            
              Application.Wait (Now + TimeValue("0:00:02"))
    
    
        Next
        
           Set AcroAVDoc = Nothing
        Set AcroPDDoc = Nothing
        appObj.Exit
        'Destroy objects
    
    
        
        'Excel environment - restore
    '    With Application
    '        .ScreenUpdating = True
    '        .DisplayAlerts = True
    '        .EnableEvents = True
    '        .Calculation = xlCalculationAutomatic
    '    End With
    
    
    End Sub
    Why only the first one is okay, another is not working. Thanks!
    By using this
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("PDF_Search")
      
        Dim a As Integer
        Dim searchString As String
        Dim blnSearch As Boolean
        Dim AcroApp As Acrobat.CAcroApp
        Dim AcrobatDoc As Acrobat.CAcroPDDoc
        Dim AcroAVDoc As Object
        
        
     For a = 401 To 1000
     
         ThisWorkbook.FollowHyperlink ws.Cells(a, 1).Value
         searchString = ws.Cells(a, 3).Value
     
             Set AcroApp = Nothing
             Set AcrobatDoc = Nothing
             Set AcroAVDoc = Nothing
    
    
             Set AcroApp = CreateObject("AcroExch.App")
             Set AcrobatDoc = CreateObject("AcroExch.PDDoc")
    
    
            
                 AcroApp.Hide
                    AcrobatDoc.Open (ws.Cells(a, 1).Value)
      
            Set AcroAVDoc = AcrobatDoc.OpenAVDoc("")
            
                    searchString = ws.Cells(a, 3).Value
                                    
                                    
                    blnSearch = AcroAVDoc.findText(szText:=searchString, _
                                                  bCaseSensitive:=False, _
                                                  bWholeWordsOnly:=True, _
                                                  bReset:=2)
                                                  
                     ws.Cells(a, 4).Value = blnSearch
                    
    
    
                    AcrobatDoc.Close
                    AcroApp.Exit
                    
    
    
           
    Next a
    Application.ScreenUpdating = False

Posting Permissions

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