Visual Basic JSO PDF突然关闭

时间:2016-07-14 05:56:12

标签: vba ms-access pdf access-vba

我在Access中获得了以下代码,可以打开并搜索400页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)
                        Set JSO = Nothing
                        Set PDDoc = Nothing
                        Set AVDoc = Nothing
                        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

适用于约200页的pdf。通过搜索400页文档,搜索约100页后pdf将突然关闭。任何人都知道问题是什么?

0 个答案:

没有答案