0

The VBA code is iterating over all pages in the PDF document using acrobat library. For each page it is iterating over all the words in that page and checking if the current word is equal to the searched word "pdf". If it is true, Then it adds the start page and end page pair to the pageArray array.

It uses another loop to iterate over all the pairs in the array and extract the pages using the jSo.extractPages method and saving each extracted pages as a new file with a new name and an incrementing index "EX_0.pdf", "EX_1.pdf" etc. The code will continue to do this until it reaches the last page of the document and extract all the pages that contain the word "pdf" into new documents.

however I am having trouble in redim array and getting script out of range error 9. on the line

ReDim Preserve pageArray(0 To UBound(pageArray, 1) + 1, 0 To 1) As Long

complete code :

Sub TwoDimensionArry()
    
    Dim pageArray As Variant
    ReDim pageArray(0 To 0, 0 To 1) As Long
    
    Dim i As Long
    Dim j As Long
    Dim stringToSearchFor As String
    Dim startPage As Long
    Dim endPage As Long
    
    stringToSearchFor = "pdf"
    
    Set AcroApp = CreateObject("AcroExch.App")
    Set acroAVDoc = CreateObject("AcroExch.AVDoc")
    
    acroAVDoc.Open "C:\Users\MBA\Desktop\Split PDF Docx live again\PDF Project 2\Murdoch_Michael__Hilary_PIA_19.pdf", ""
    
    Set acroPDDoc = acroAVDoc.GetPDDoc
    Set jSo = acroPDDoc.GetJSObject
    
    startPage = 0
    endPage = 0
    
    For i = 0 To acroPDDoc.GetNumPages() - 1
        
        numWords = jSo.GetPageNumWords(i)
        
        For j = 0 To numWords - 1
        
            If jSo.GetPageNthWord(i, j) = stringToSearchFor Then
            
                ' Check if this is the first page with the keyword
                If started = False Then
                    startPage = i
                    started = True
                Else
                    endPage = i - 1
                    
                    'ERROR-9 LINE - Subscript out of Range
                    ' Add the start page and end page to the array
                    ReDim Preserve pageArray(0 To UBound(pageArray, 1) + 1, 0 To 1) As Long
    
                    pageArray(UBound(pageArray, 1), 0) = startPage
                    pageArray(UBound(pageArray, 1), 1) = endPage
                    ' Update the start page for the next pair
                    startPage = i
                    started = False
                End If
                Exit For
            End If
        Next j
    Next i
    
    ' Add the last page as the end page for the last pair
    endPage = acroPDDoc.GetNumPages()

ReDim Preserve pageArray(0 To UBound(pageArray, 1) + 1, 0 To 1) As Long
                    pageArray(UBound(pageArray, 1), 0) = startPage
                    pageArray(UBound(pageArray, 1), 1) = endPage


    ' Check if there are any pairs in the array
    If UBound(pageArray, 1) > 0 Then
        ' Iterate over all the pairs in the array
        For i = 0 To UBound(pageArray, 1)
            ' Extract the pages using the jso.extractPages method
            jSo.extractPages pageArray(i, 0), pageArray(i, 1), "C:\Users\MBA\Desktop\Split PDF Docx live again\PDF Project 2\EX" & i & ".pdf"
        Next i
    End If

End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
VBAbyMBA
  • 806
  • 2
  • 12
  • 30
  • You can´t redim a 2d array's first index. [Check this question](https://stackoverflow.com/questions/13183775/excel-vba-how-to-redim-a-2d-array) – cyberponk Jan 23 '23 at 20:07

0 Answers0