VBA Code to Extract Text from a PDF File in Excel

Extracting text from a PDF file directly using VBA in Excel is not a built-in functionality. However, you can achieve this by utilizing third-party libraries or tools. One popular library is “Adobe Acrobat” that provides an API called “Adobe Acrobat DC SDK” which can be used to extract text from PDF files programmatically.

Here’s an example of VBA code that uses the Adobe Acrobat library to extract text from a PDF file:

VBA Code:

Sub ExtractTextFromPDF()
    Dim acApp As Object ' Adobe Acrobat application object
    Dim acDoc As Object ' Adobe Acrobat document object
    Dim acPage As Object ' Adobe Acrobat page object
    Dim text As String ' Variable to store extracted text
    ' Create new instances of Adobe Acrobat objects
    Set acApp = CreateObject("AcroExch.App")
    Set acDoc = CreateObject("AcroExch.PDDoc")
    ' Open the PDF file
    If acDoc.Open("C:\Path\to\your\file.pdf") Then
        ' Extract text from each page
        For i = 0 To acDoc.GetNumPages - 1
            Set acPage = acDoc.AcquirePage(i)
            text = text & acPage.GetWordText & vbCrLf
        Next i
        ' Display the extracted text
        MsgBox text
        ' Close the PDF file
        MsgBox "Failed to open the PDF file."
    End If
    ' Release the Adobe Acrobat objects
    Set acPage = Nothing
    Set acDoc = Nothing
    Set acApp = Nothing
End Sub

Check All VBA Codes

Join Our Telegram Group techguruplus telegram group Join Our WhatsApp Group techguruplus whatsapp group

Leave a Comment