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
        acDoc.Close
    Else
        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

Join the TechGuruPlus Telegram group

Join Our WhatsApp Group

Join the TechGuruPlus WhatsApp group
Nazim Khan, founder of TechGuruPlus.com

— Founder & Editor, TechGuruPlus.com
[MBA in Finance]

Nazim Khan has spent over ten years working with Excel, Tally and office documentation in accounts, finance and MIS roles. He has been running TechGuruPlus.com since 2016, where he publishes free editable templates for office work, and the YouTube channel Business Excel. He has trained more than 50,000 people online. Every template on this site is built and checked by him before it is published.

Contact  Â·  Excel Course

Leave a Comment