Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for converting pdf to diff formats -not working

Tags:

vba

Following is VBA code for converting PDF to different formats (.xls, .xlsx and .doc etc)

Not getting compiled, failing with ERROR - USER DEFINED TYPE NOT DEFINED .

Code

Option Explicit
Option Private Module

Sub SavePDFAsOtherFormat(PDFPath As String, FileExtension As String)

'Saves a PDF file as another format using Adobe Professional.


Dim objAcroApp      As Acrobat.AcroApp
Dim objAcroAVDoc    As Acrobat.AcroAVDoc
Dim objAcroPDDoc    As Acrobat.AcroPDDoc
Dim objJSO          As Object
Dim boResult        As Boolean
Dim ExportFormat    As String
Dim NewFilePath     As String

'Check if the file exists.
If Dir(PDFPath) = "" Then
    MsgBox "Cannot find the PDF file!" & vbCrLf & "Check the PDF path and retry.", _
            vbCritical, "File Path Error"
    Exit Sub
End If

'Check if the input file is a PDF file.
If LCase(Right(PDFPath, 3)) <> "pdf" Then
    MsgBox "The input file is not a PDF file!", vbCritical, "File Type Error"
    Exit Sub
End If

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

'Set AVDoc object.
Set objAcroAVDoc = CreateObject("AcroExch.AVDoc")

'Open the PDF file.
boResult = objAcroAVDoc.Open(PDFPath, "")

'Set the PDDoc object.
Set objAcroPDDoc = objAcroAVDoc.GetPDDoc

'Set the JS Object - Java Script Object.
Set objJSO = objAcroPDDoc.GetJSObject

'Check the type of conversion.
Select Case LCase(FileExtension)
    Case "eps": ExportFormat = "com.adobe.acrobat.eps"
    Case "html", "htm": ExportFormat = "com.adobe.acrobat.html"
    Case "jpeg", "jpg", "jpe": ExportFormat = "com.adobe.acrobat.jpeg"
    Case "jpf", "jpx", "jp2", "j2k", "j2c", "jpc": ExportFormat = "com.adobe.acrobat.jp2k"
    Case "docx": ExportFormat = "com.adobe.acrobat.docx"
    Case "doc": ExportFormat = "com.adobe.acrobat.doc"
    Case "png": ExportFormat = "com.adobe.acrobat.png"
    Case "ps": ExportFormat = "com.adobe.acrobat.ps"
    Case "rft": ExportFormat = "com.adobe.acrobat.rft"
    Case "xlsx": ExportFormat = "com.adobe.acrobat.xlsx"
    Case "xls": ExportFormat = "com.adobe.acrobat.spreadsheet"
    Case "txt": ExportFormat = "com.adobe.acrobat.accesstext"
    Case "tiff", "tif": ExportFormat = "com.adobe.acrobat.tiff"
    Case "xml": ExportFormat = "com.adobe.acrobat.xml-1-00"
    Case Else: ExportFormat = "Wrong Input"
End Select

'Check if the format is correct and there are no errors.
If ExportFormat <> "Wrong Input" And Err.Number = 0 Then

    'Format is correct and no errors.

    'Set the path of the new file. Note that Adobe instead of xls uses xml files.
    'That's why here the xls extension changes to xml.
    If LCase(FileExtension) <> "xls" Then
        NewFilePath = WorksheetFunction.Substitute(PDFPath, ".pdf", "." & LCase(FileExtension))
    Else
        NewFilePath = WorksheetFunction.Substitute(PDFPath, ".pdf", ".xml")
    End If

    'Save PDF file to the new format.
    boResult = objJSO.SaveAs(NewFilePath, ExportFormat)

    'Close the PDF file without saving the changes.
    boResult = objAcroAVDoc.Close(True)

    'Close the Acrobat application.
    boResult = objAcroApp.Exit

    'Inform the user that conversion was successfully.
    MsgBox "The PDf file:" & vbNewLine & PDFPath & vbNewLine & vbNewLine & _
    "Was saved as: " & vbNewLine & NewFilePath, vbInformation, "Conversion finished successfully"

Else

    'Something went wrong, so close the PDF file and the application.

    'Close the PDF file without saving the changes.
    boResult = objAcroAVDoc.Close(True)

    'Close the Acrobat application.
    boResult = objAcroApp.Exit

    'Inform the user that something went wrong.
    MsgBox "Something went wrong!" & vbNewLine & "The conversion of the following PDF file FAILED:" & _
    vbNewLine & PDFPath, vbInformation, "Conversion failed"

End If

'Release the objects.
Set objAcroPDDoc = Nothing
Set objAcroAVDoc = Nothing
Set objAcroApp = Nothing

End Sub
like image 682
VIMAL RAJ Avatar asked Apr 24 '26 13:04

VIMAL RAJ


1 Answers

I never have been able to get a code to work to save as a jpg. I can use similar code to save/export as text but the jpg code fails each time.

EDIT (RESOLVED):

I know this is a very old thread but I FINALLY figured it out. LOL I did not use the code above because I was only interested in saving a PDF document as jpeg. When this is done manually, you will get one jpg file for EVERY page in the PDF. (That is what I was trying to do).

It seems the javascript that is used inside MS Access to run Acrobat has a unique method to post the save path. When the correct format is not used, an error is generated. But when formatted correctly, it works.

I have posted the code I use below. Any 'pro' comments or ways to improve are appreciated.

Private Sub PDFtoJPG_Click()

Dim AcroXApp As Acrobat.acroApp
Dim AcroXAVDoc As Acrobat.AcroAVDoc
Dim AcroXPDDoc As Acrobat.AcroPDDoc

Dim Filename As String, jsObj As Object


Filename = "D:\Movie\PDF VBA\PDF Results.PDF"  '<<ENTER THE FILEMNAME AND PATH HERE

Set AcroXApp = CreateObject("AcroExch.App")
AcroXApp.Show
Set AcroXAVDoc = CreateObject("AcroExch.AVDoc")
AcroXAVDoc.Open Filename, "Acrobat"
Set AcroXPDDoc = AcroXAVDoc.GetPDDoc
Set jsObj = AcroXPDDoc.GetJSObject

'Notice the path is not the standard MS Windows format
jsObj.saveAs "/D/Movie/PDF VBA/PDF Results.jpg", "com.adobe.acrobat.jpeg"

AcroXAVDoc.Close False
AcroXApp.Hide
AcroXApp.Exit

MsgBox "PDF File Saved as JPG Images "

End Sub

like image 140
DavidHP Avatar answered Apr 28 '26 21:04

DavidHP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!