(Solved) – How do you skip password protected pdf’s in VBA, when running a macro loop?

  • by

I have 75000 pdfs i’m cycling through using a VBA Excel program. My program stops when it reaches a pdf which requires a password (which I don’t have). How do I skip over such files without my program coming to a halt? I had an extensive look on google and could find nothing on this.


Edit: added below code. The full path is brought into this function from a list on a worksheet. As the function is opening the PDF’s here, I guess it’s somewhere here I need to try to find a way of skipping over password protected pdf’s? Any advice greatly appreciated.

Code below:

Function getTextFromPDF(ByVal strFileName As String) As String
   Dim objAVDoc As New AcroAVDoc
   Dim objPDDoc As New AcroPDDoc
   Dim objPage As AcroPDPage
   Dim objSelection As AcroPDTextSelect
   Dim objHighlight As AcroHiliteList
   Dim pageNum, tCount As Long
   Dim StrTxtt As String

   StrTxtt = ""
   On Error GoTo errhandler
   If (objAVDoc.Open(strFileName, "")) Then
      Set objPDDoc = objAVDoc.GetPDDoc
      For pageNum = 0 To objPDDoc.GetNumPages() - 1
         Set objPage = objPDDoc.AcquirePage(pageNum)
         Set objHighlight = New AcroHiliteList
         objHighlight.Add 0, 10000
         Set objSelection = objPage.CreatePageHilite(objHighlight)

         If Not objSelection Is Nothing Then
            For tCount = 0 To objSelection.GetNumText - 1
               StrTxtt = StrTxtt & objSelection.GetText(tCount)
            Next tCount
         End If
         'change pages from low to high eg, 10, 50, 100, 200 etc.

      Next pageNum
      objAVDoc.Close 1
   End If
   getTextFromPDF = StrTxtt
   Exit Function
    objAVDoc.Close 1
    getTextFromPDF = "error opening pdf"
    On Error GoTo 0
    Exit Function
End Function

Leave a Reply

Your email address will not be published. Required fields are marked *