2017-03-06 3 views
3

Es folgt VBA-Code PDF in verschiedenen Formaten (XLS, XLSX und .doc usw.) pdf zu diff Formate konvertieren-Code für -keine Arbeits

zum Umwandeln

nicht kompiliert zu werden, mit ERROR Fehler - 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 
+0

Haben Sie Adobe Professional? – 0m3r

+0

ya benutze ich Adobe Professional 7.0 Version –

Antwort

0

Sie sollten jede referenzierte Objekt als Variant

erklären

Siehe msdn für Variantentyp und ähnlich so post

eine Datei von einem Ordner Desktop Ersten

Dim directory As String, fileName As String 
    Dim fd As Office.FileDialog 

    Set fd = Application.FileDialog(msoFileDialogFilePicker) 

    With fd 
    .AllowMultiSelect = False 
    .Title = "Please select the file." 
    .Filters.Clear 
    .Filters.Add "Excel 2003", "*.xls?" 

    If .Show = True Then 
     fileName = Dir(.SelectedItems(1)) // you have file name here 
    boResult = objAcroAVDoc.Open(fileName, "") 
    End If 
    End With 

Für fest codierten Pfad erklären globale Variable über Ihrer Funktion, dann brauchen Sie nicht um dies als Variable zu übergeben.

Public PDFPath As String 
Sub SavePDFAsOtherFormat(FileExtension As String) 
+0

Hallo Anil. Danke. Möchte wissen, wie man den Pfad der pdf-Dateien/Ordner in meinem Desktop in dem gegebenen Makro definiert. –

+0

siehe https://msdn.microsoft.com/en-us/library/office/ff868990.aspx – Anil

+0

hii anil .. Danke .. Aber was ich wissen will, ist im Makro in meiner Frage geschrieben, möchte ich Zeigen Sie den Datei-/Ordnerpfad an. Wohin soll ich den Code hinzufügen/ändern, damit das Makro funktioniert? –

0

Um das Makro verwenden Sie den Acrobat-Bibliothek von VBA-Editor aktivieren müssen

Go to Tools -> References -> Adobe Acrobat xx.0 Type Library, where xx depends on your Acrobat Professional version (i.e. 9.0 or 10.0) you have installed to your PC.

Alternativ können Sie es Tools -> References -> Browse und überprüfen Sie die finden Pfad C:\Program Files\Adobe\Acrobat xx.0\Acrobat\acrobat.tlb, wobei x x ist Ihre Acrobat-Version (d. h. 9.0 oder 10.0 etc.).