VB使用JSO在Access中搜索PDF

时间:2016-07-10 06:02:03

标签: vba pdf access

我在Access中获得了以下代码,可以打开并搜索1300页PDF。

    Option Explicit

Sub SearchWordInPDF()
    'Declaring the necessary variables.
    Dim WordToFind  As String
    Dim PDFPath     As String
    Dim App         As Object
    Dim AVDoc       As Object
    Dim PDDoc       As Object
    Dim JSO         As Object
    Dim i           As Long
    Dim j           As Long
    Dim Word        As Variant
    Dim Result      As Integer

PDFPath = "Enter Path"

'Check if the file exists.
If Dir(PDFPath) = "" Then
    MsgBox "Cannot find the PDF file!" & vbCrLf & "Check the PDF path and retry.", _
            vbCritical, "File Path Error"
    Exit Sub
End If

'Check if the input file is a PDF file.
If LCase(Right(PDFPath, 3)) <> "pdf" Then
    MsgBox "The input file is not a PDF file!", vbCritical, "File Type Error"
    Exit Sub
End If

On Error Resume Next

'Initialize Acrobat by creating the App object.
Set App = CreateObject("AcroExch.App")

'Check if the object was created. In case of error release the objects and exit.
If Err.Number <> 0 Then
    MsgBox "Could not create the Adobe Application object!", vbCritical, "Object Error"
    Set App = Nothing
    Exit Sub
End If

'Create the AVDoc object.
Set AVDoc = CreateObject("AcroExch.AVDoc")

'Check if the object was created. In case of error release the objects and exit.
If Err.Number <> 0 Then
    MsgBox "Could not create the AVDoc object!", vbCritical, "Object Error"
    Set AVDoc = Nothing
    Set App = Nothing
    Exit Sub
End If

On Error GoTo 0

'Open the PDF file.
If AVDoc.Open(PDFPath, "") = True Then

    'Open successful, bring the PDF document to the front.
    AVDoc.BringToFront

    'Set the PDDoc object.
    Set PDDoc = AVDoc.GetPDDoc

    'Set the JS Object - Java Script Object.
    Set JSO = PDDoc.GetJSObject

    'Search for the word.
    If Not JSO Is Nothing Then

        'Loop through all the pages of the PDF.
        For i = 0 To JSO.numPages - 1

            'Loop through all the words of each page.
            For j = 0 To JSO.getPageNumWords(i) - 1

                'Get a single word.
                Word = JSO.getPageNthWord(i, j)

                'If the word is string...
                If VarType(Word) = vbString Then

                    'Compare the word with the text to be found.
                    Result = StrComp(Word, WordToFind, vbTextCompare)

                    'If both strings are the same.
                    If Result = 0 Then
                        'Select the word and exit.
                        Call JSO.selectPageNthWord(i, j)
                        Exit Sub
                    End If

                End If

            Next j

        Next i

        'Word was not found, close the PDF file without saving the changes.
        AVDoc.Close True

        'Close the Acrobat application.
        App.Exit

        'Release the objects.
        Set JSO = Nothing
        Set PDDoc = Nothing
        Set AVDoc = Nothing
        Set App = Nothing

        'Inform the user.
        MsgBox "The word '" & WordToFind & "' could not be found in the PDF file!", vbInformation, "Search Error"

    End If

Else

    'Unable to open the PDF file, close the Acrobat application.
    App.Exit

    'Release the objects.
    Set AVDoc = Nothing
    Set App = Nothing

    'Inform the user.
    MsgBox "Could not open the PDF file!", vbCritical, "File error"

End If 
End Sub

它适用于pdf的前300-400页但是它遇到问题并且在for循环中不会将j重置为0,从而将进程踢出循环。我会在没有做任何事情的情况下数到文件的末尾,因为j已经在&#34; JSO.getPageNumWords(i) - 1&#34;价值和过程结束。我在Excel中运行它并且工作正常但由于某种原因它在Access中无法工作。任何人都知道问题是什么?

0 个答案:

没有答案