Extract Images from a PDF using VBA

CraigHK

New Member
Joined
Feb 15, 2020
Messages
1
Office Version
2013
Platform
Windows
I am trying to extract images from a PDF in Excel VBA. I am able to extract all of the text page by page with no problem (code below). I have searched for sample code or an explanation of how to extract images but the only sample code I found did not work. Does anybody know how to extract images one by one from a PDF and save each image to a folder (which would be specified in the VBA code) using just VBA (without any third party software)? I would appreciate any information.

VBA Code:
Sub GetText()

Dim aApp As Acrobat.AcroApp
Dim av_doc As AcroAVDoc
Dim pdf_doc As AcroPDDoc
Dim sel_text As AcroPDTextSelect
Dim X as Variant
Dim Y As Long
Dim pageNumber, pageContent, content

Set aApp = CreateObject("AcroExch.App") Set av_doc = CreateObject("AcroExch.AVDoc")
If av_doc.Open("C:\VBA\PDF\Pages.pdf", vbNull) <> True Then Stop

Set av_doc = aApp.GetActiveDoc
Set pdf_doc = av_doc.GetPDDoc

' Iterate through the document page by page
For X = 0 To pdf_doc.GetNumPages - 1

Set pageNumber = pdf_doc.AcquirePage(X)
Set pageContent = CreateObject("AcroExch.Hilitelist")

On Error Resume Next
If pageContent.Add(0, 9000) <> True Then Stop
Set sel_text = pageNumber.CreatePageHilite(pageContent)
On Error GoTo 0

' Iterate through page word by word
For Y = 0 To sel_text.GetNumText - 1

' Display each word in a cell on spreadsheet
Cells(Y + 1, X + 1) = sel_text.GetText(Y)

Next Y
Next X

'Close and Release
av_doc.Close False
aApp.Exit
Set sel_text = Nothing
Set pdf_doc = Nothing
Set av_doc = Nothing
Set pageNumber = Nothing

End Sub
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
41,261
Office Version
365
Platform
Windows
Cross posted Extract Images from a PDF using VBA

While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 

Watch MrExcel Video

Forum statistics

Threads
1,100,142
Messages
5,472,754
Members
406,835
Latest member
steve43040

This Week's Hot Topics

Top