Extract images from PDF using VBA

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Extract images from PDF using VBA

Post by YasserKhalil »

Hello everyone
I am searching for this topic and I have found a VBSCript that works fine .. but I need to do that using VBA not VBS

Code: Select all

' Create Bytescout.PDFExtractor.ImageExtractor object
Set extractor = CreateObject("Bytescout.PDFExtractor.ImageExtractor")
extractor.RegistrationName = "demo"
extractor.RegistrationKey = "demo"
 
' Load sample PDF document
extractor.LoadDocumentFromFile("nn.pdf")
 
' Get page count
pageCount = extractor.GetPageCount()
         
' Extract images from each page
For i = 0 To pageCount - 1
 
    j = 0
     
    ' Initialize page images enumeration
    If extractor.GetFirstPageImage(i) Then
        Do
            outputFileName = "page" & i & "image" & j & ".png"
 
            ' Save image to file
            extractor.SaveCurrentImageToFile outputFileName
 
            j = j + 1
 
        Loop While extractor.GetNextImage() ' Advance image enumeration
    End If
Next
 
' Open first output file in default associated application
Set shell = CreateObject("WScript.Shell")
shell.Run "page0image0.png", 1, false
Set shell = Nothing
 
Set extractor = Nothing
I encountered an error at this line:
Set shell = CreateObject("WScript.Shell")

The code is taken from this link
https://bytescout.com/products/develope ... d-VBScript+

Thanks advanced for help

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Extract images from PDF using VBA

Post by HansV »

1) Did you install the Bytescout PDF Extractor SDK?
2) Did you declare the variable Shell? If so, how?
3) VBA has a built-in instruction Shell, perhaps you should give the variable in your code a different name.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Extract images from PDF using VBA

Post by YasserKhalil »

Yes the Bytescout PDF Extractor SDK is installed.
I just copied the same code for VBScript and out in VBA ..and commented out Option Explicit

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Extract images from PDF using VBA

Post by HansV »

What was the error message that you got?
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Extract images from PDF using VBA

Post by YasserKhalil »

Argument not optional. It didn't execute a line even. Just received the message directly when trying to press F5