Extract Images from a PDF using VBA

CraigHK

New Member
Joined
Feb 15, 2020
Messages
1
Office Version
  1. 2013
Platform
  1. 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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
50,563
Office Version
  1. 365
Platform
  1. 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.
 

chnoir

New Member
Joined
Oct 29, 2020
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I have the same problem that you had in february, did you find the coding to extract the images and can you help me on this matter. I am belgian and not profesional on this matther
Thank you in advance
Christian
 

Watch MrExcel Video

Forum statistics

Threads
1,118,138
Messages
5,570,394
Members
412,321
Latest member
Yusuf_A
Top