Quantcast
Channel: Active questions tagged filereader - Stack Overflow
Viewing all articles
Browse latest Browse all 53

How to fix macro used to find phrase within multiple PDF documents

$
0
0

I currently have a macro to read PDFs but the issue I am having is it is NOT finding the specific "Text to Search for" and providing incorrect results. I have my PDFs saved in a folder and even when I have just a few files, sometimes there is an "x" in "Authorized Brand Alternative" even though that text is not found on the PDF. It also takes quite a while to even read four files...

Below is the macro and I've included a screenshot of my macro file Enter_Text tab and Analysis tab (results screen). I created 4 PDF documents with different scenarios, saved them into a folder, ran the macro and results are shown in screenshot. As you can see, The first example and 4th example were read but the other two did not return correct results.

PDF Reader screenshot

What might be the issue?

Option Base 1Dim PhraseLabel(25), PhraseText(25) As StringSub FindPDFtextB()'' Opens and searches a PDF file for specified text'Dim TargetFolder, PDFname, PDFpath As String  ' Path to files to be searchedDim foundText As Long ' Return value from FindText methodDim iFiles, numPhr, i As Integer'Dim TxtB As Range' Get number of text clauses to search forWorksheets("Enter_Text").ActivateRange("A6").SelectSelection.End(xlDown).SelectnumPhr = ActiveCell.Row - Range("TextLabel").Row' Read text phrases into array    For i = 1 To numPhr        PhraseLabel(i) = Worksheets("Enter_Text").Range("TextLabel").Offset(i, 0).Value        PhraseText(i) = Worksheets("Enter_Text").Range("TextList").Offset(i, 0).Value    Next i' Output goes on the Analysis worksheetWorksheets("Analysis").Activate    Set TxtB = Worksheets("Analysis").Range("TextCheck")    For i = 1 To numPhr        TxtB.Offset(0, i - 1).Select        With Selection            .Value = PhraseLabel(i)            .HorizontalAlignment = xlGeneral            .VerticalAlignment = xlBottom            .WrapText = False            .Orientation = 45        End With    Next i' Set file counteriFiles = 1' Get folder path from worksheetTargetFolder = Worksheets("Enter_Text").Range("FolderToTestB")If Right(TargetFolder, 1) \<\> "" Then TargetFolder = TargetFolder & ""' Set target folder objectsSet tgtObject = New Scripting.FileSystemObjectSet tgtSource = tgtObject.GetFolder(TargetFolder)'    On Error Resume NextApplication.DisplayAlerts = FalseFor Each myFile In tgtSource.Files' Initialize Acrobat by creating App objectSet tgtApp = CreateObject("AcroExch.App", "")'        tgtApp.HideSet tgtAvDoc = CreateObject("AcroExch.AVDoc")        PDFname = myFile.Name        PDFpath = TargetFolder & PDFname        Range("FileNameB").Offset(iFiles, 0).Value = PDFname' open the PDFIf tgtAvDoc.Open(PDFpath, "") Then            On Error Resume Next            For i = 1 To numPhr                foundText = tgtAvDoc.FindText(PhraseText(i), caseSensitive, WholeWords, bReset)  'Returns -1 if found, 0 otherwise                If foundText = -1 Then                    Range("TextCheck").Offset(iFiles, i - 1).Value = "X"                Else                    Range("TextCheck").Offset(iFiles, i - 1).Value = ""                End If            On Error Resume Next            Next i            On Error Resume Next            tgtApp.getActiveDoc.Close True            Set tgtApp = Nothing        Else ' if failed, show error message            Resp = MsgBox("Cannot open " & PDFpath, vbOKOnly)        End If        iFiles = iFiles + 1    NextEnd Sub

I have never worked with macros and this is a reader that my predecessor put together.


Viewing all articles
Browse latest Browse all 53

Latest Images

Trending Articles





Latest Images