• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5876
  • Last Modified:

Convert PDF to JPG using VBA

I am needing to convert a large number of pdf files into jpg format. The files are located in separate folders on the server, however the folders contain a mix of pdf and jpg already.   Since the database that manages the files and folders is Access 2007 it would be ideal to be able to make this conversion work using VBA.  I recognize that there are dozens of freeware third party applications that can do this but I think I need more control of the process as the mix of pdf and jpg files would likely screw up batch conversions.  Also, new documents being entered into the system by necessity are pdf.  So in addition to a large batch process I would like to be able to convert files on the fly so that all of the files in each folder are all jpg and eliminate the collage.

I have Adobe Acrobat X installed on the server and this application can easily perform the conversion.  The following code is a snippet of VB6 code that I have attempted to convert to VBA but am getting errors on several remaining lines.

Any help would be appreciated.

Dim doc As Acrobat.AcroPDDoc
Dim page As Acrobat.AcroPDPage
Dim pt As Acrobat.AcroPoint
Dim rect As Acrobat.AcroRect
Dim data As IDataObject '<===== Error in VBA
Dim bmp As Bitmap  '<===== Error in VBA



                doc = New Acrobat.AcroPDDoc
                page = Nothing
                doc.Open ("C:\Documents and Settings\tk46261\My Documents\sickdays.pdf")
                page = doc.AcquirePage(0)
                pt = page.GetSize()
                rect = New Acrobat.AcroRect
                rect.Top = 0
                rect.Left = 0
                rect.Right = pt.x
                rect.bottom = pt.y
               
                page.CopyToClipboard(rect, 0, 0, 100) '<===== Error in VBA
               
                data = Clipboard.GetDataObject() '<===== Error in VBA
                bmp = data.GetData(DataFormats.Bitmap) '<===== Error in VBA
                bmp.Save ("C:\Documents and Settings\tk46261\Desktop\Print Excel To JPG\test.jpeg") '<===== Error in VBA
0
isurgyn
Asked:
isurgyn
  • 9
  • 4
1 Solution
 
MlandaTCommented:
What error message do you get?
0
 
isurgynAuthor Commented:
There are multiple errors.  Although I am not an expert in vb, it appears that the Objects are not supported in vba as it is a less powerful language than vb.

That said, after doing more research it appears that there are some good references on automating Adobe Acrobat using vb and I am not so certain that the method used in the code snippet above is the best alternative.  I am posting the best links I have found so far here and I will continue to work on the code to refine it.

www.khk.net/wordpress/2009/03/04/adobe-acrobat-and-vba-an-introduction/

The downloads of the following documents are present on that website.

Developing Applications Using Interapplication Communication (510KB PDF Document)

Interapplication Communication API Reference (2.3MB PDF Document)

I am working through the commands and if I can't get it will repost new a new code snippet.  So unless you have experience with the Acrobat SDK in vba let me sort through this a bit more and then start again if I have more issues.
0
 
MlandaTCommented:
The problem lines you have indicated there are actually "not" Adobe Acrobat SDK code. Hence I was asking what the exact error message you were receiving was. By getting an exact error message, it becomes easier to offer you assitance. No need to make conjectures about whether or not I have any experience with Acrobat SDK, which I do. Anyway, that said, the lines:
Dim data As IDataObject '<===== Error in VBA (I'll assume error says something like Type 'IDataObject' is not defined)
Dim bmp As Bitmap  '<===== Error in VBA

Open in new window

Suggest missing references in your Access macros. Try adding a reference System_Windows_Forms (System.Windows.Forms.dll) and System.Drawing.dll adding references
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
isurgynAuthor Commented:
Thank you so much and sorry for the assumptions.  I had begun to think that the code was just too flawed to salvage and would need to do more work myself before anyone could help.

I fixed the missing references and the code is much better but still am getting two errors.

My current code version:

Dim doc As Acrobat.AcroPDDoc
Dim page As Acrobat.AcroPDPage
Dim pt As Acrobat.AcroPoint
Dim rect As Acrobat.AcroRect
Dim data As IDataObject
Dim bmp As Bitmap

Dim app As Object
Dim avdoc As Object
Dim pageview As Object

Set app = CreateObject("AcroExch.App")
Set avdoc = app.GetActiveDoc
Set pageview = avdoc.GetAVPageView
pageview.Goto (1)


                doc = New Acrobat.AcroPDDoc
                page = Nothing
                doc.Open ("C:\Patient Files\08808\Testpdf.pdf")
                page = doc.AcquirePage(0)
                pt = page.GetSize()
                rect = New Acrobat.AcroRect
                rect.Top = 0
                rect.Left = 0
                rect.Right = pt.x
                rect.bottom = pt.y
               
                page.CopyToClipboard(rect, 0, 0, 100) '<===== Error in VBA
               
                data = Clipboard.GetDataObject()
                bmp = data.GetData(DataFormats.Bitmap)
                bmp.Save ("C:\Patient Files\08808\Testjpg.jpg) '<===== Error in VBA
               
The error on the page.CopyToClipboard error(s) - Compile error: "Expected = " and also get "Syntax Error" during code execution.

The error on the bmp.Save line  is "Variable not defined".
0
 
MlandaTCommented:
First one is due to VBA syntax:
Dim PageCopied As Boolean
PageCopied = page.CopyToClipboard(rect, 0, 0, 100)

OR

page.CopyToClipboard rect, 0, 0, 100   '<===NOTE no brackets

Second one
Apart from a missing " in the code (after the .jpg) you pasted above, I cannot quite see anything wrong with it.
                bmp.Save ("C:\Patient Files\08808\Testjpg.jpg") '<===== Error in VBA
0
 
isurgynAuthor Commented:
Ok those errors are cleared.  Sorry about the typo on the second line.

I now get a "Variable not defined" error on the

Data = Clipboard.GetDataObject()

The "Clipboard" term is highlighted.

I think we are almost there. Thank you so much!
0
 
MlandaTCommented:
Change it to
Data = System.Windows.Forms.Clipboard.GetDataObject()

Open in new window

0
 
isurgynAuthor Commented:
Same Compile error - Variable not defined
Now System is highlighted in debug
0
 
isurgynAuthor Commented:
This link suggests that VBA may not be able to handle non-text paste from Clipboard.  That said, this VB code is definitely beyond my pay scale so I humbly await your direction.

www.cpearson.com/excel/clipboard.aspx
0
 
isurgynAuthor Commented:
As an alternative solution I have been attempting to create a routine that will automate Acrobat and Save the open pdf as a JPG image using MenuItemExecute commands by attempting to emulate the steps that can be performed manually.  Although the code works, there is no obvious method to include a file name or path on the  AcroApp.MenuItemExecute ("SaveAs") menu item.  

Failing that, I have tried AcroPDDoc.Save command but still no success.


Private Sub cmdAutomateAcrobat_Click()

Dim AcroApp As Acrobat.AcroApp
Dim AcroAVDoc As Acrobat.AcroAVDoc
Dim AcroPDDoc As Acrobat.AcroPDDoc
Dim gPDFPath As String

gPDFPath = "C:\Patient Files\08808\Testpdf.pdf"

' ** Initialize Acrobat by creating App object
Set AcroApp = CreateObject("AcroExch.App")

' ** show Acrobat
AcroApp.Show

' ** Set AVDoc object
Set AcroAVDoc = CreateObject("AcroExch.AVDoc")

' ** open the PDF
    If AcroAVDoc.Open(gPDFPath, vbNull) <> True Then
        Exit Sub
    End If
   
    Set AcroAVDoc = AcroApp.GetActiveDoc
    Set AcroPDDoc = AcroAVDoc.GetPDDoc

    AcroApp.MenuItemExecute ("SaveAs") '<====No obvious submenu to define file name and path but code does properly opens the SaveAs window

    AcroPDDoc.Save(PDSaveFull, "C:\Patient Files\08808\Testjpg.jpg")  '<==== This code won't work either as it returns the error "Expected = "
   
 MsgBox "This step should Save As JPEG" ' stop program from closing docs until I figure out how to SaveAs JPEG
   
AcroApp.CloseAllDocs
AcroApp.Exit

Set AcroApp = Nothing
Set AcroAVDoc = Nothing
Set AcroPDDoc = Nothing

End Sub
0
 
isurgynAuthor Commented:
If it matters I am using Adobe Acrobat X Standard not Professional
0
 
isurgynAuthor Commented:
OK I believe that the answers to this challenge have been demonstrated in the previous posts.

1.  VBA is not able to handle non-text paste to Clipboard
2.  The Adobe automation is not sophisticated enough to handle an auto save so requires user intervention.  OK but not useful to convert multiple files automatically.

The best control I have found is by viscomsoft.com   It is not terribly expensive and works pretty much as advertised.  I can't display multi page pdf and the print function crashes.  Otherwise it is the only 3rd party control that I can find that will actually accomplish this conversion and / or display either jpeg or pdf in the same control.
0
 
isurgynAuthor Commented:
It is my own solution.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now