【问题标题】:Code for converting pdf to diff formats -not working将 pdf 转换为 diff 格式的代码 - 不起作用
【发布时间】:2017-03-06 07:26:44
【问题描述】:

以下是用于将 PDF 转换为不同格式(.xls、.xlsx 和 .doc 等)的 VBA 代码

未编译,失败并出现错误 - USER DEFINED TYPE NOT DEFINED .

代码

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

【问题讨论】:

  • 您有 Adob​​e Professional 吗?
  • 是的,我使用的是 adobe Professional 7.0 版本

标签: vba


【解决方案1】:

我从来没有能够得到一个可以保存为 jpg 的代码。 我可以使用类似的代码来保存/导出为文本,但 jpg 代码每次都失败。

编辑(已解决):

我知道这是一个非常古老的线程,但我终于弄明白了。哈哈 我没有使用上面的代码,因为我只对将 PDF 文档保存为 jpeg 感兴趣。手动完成此操作后,您将为 PDF 中的每一页获得一个 jpg 文件。 (这就是我想要做的)。

似乎在 MS Access 中用于运行 Acrobat 的 javascript 具有发布保存路径的独特方法。如果不使用正确的格式,则会产生错误。但如果格式正确,它就可以工作。

我在下面发布了我使用的代码。感谢任何“专业”的 cmets 或改进方法。

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 "

结束子

【讨论】:

    【解决方案2】:

    要使用宏,您必须从 VBA 编辑器启用 Acrobat 库

    Go to Tools -&gt; References -&gt; 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.

    您也可以找到 Tools -&gt; References -&gt; Browse 并检查路径 C:\Program Files\Adobe\Acrobat xx.0\Acrobat\acrobat.tlb,其中 xx 是您的 Acrobat 版本(即 9.0 或 10.0 等)。

    【讨论】:

      【解决方案3】:

      您应该将每个引用的对象声明为Variant

          Dim objAcroApp      As Variant
      .....
      Set objAcroApp       = CreateObject("AcroExch.App")
      

      请参阅msdn 了解变体类型和类似的post

      从桌面文件夹中获取文件

      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
      

      对于硬编码路径,在函数上方声明全局变量,则无需将其作为变量传递。

      Public PDFPath As String 
      Sub SavePDFAsOtherFormat(FileExtension As String)
      

      【讨论】:

      • 嗨,阿尼尔。感谢你。想知道如何在给定的宏中定义我桌面中 pdf 文件/文件夹的路径。
      • hii anil.. 谢谢.. 但我想知道的是在我的问题中发布的宏中,我想显示文件/文件夹路径。我应该在哪里添加/修改代码才能使宏工作..
      • @vimalraj 你说的显示是什么意思,一个选择路径的对话框还是别的什么?
      • 在我发布的宏中,它将根据我需要在宏中提供的路径从我桌面的文件夹中选择 pdf 文件。我想知道我应该在宏中写什么代码来定义文件夹/文件路径。
      • 根据我对您的问题的理解,添加了两种定义/获取文件路径的方法。
      猜你喜欢
      • 2023-02-26
      • 2022-06-14
      • 2014-03-11
      • 1970-01-01
      • 2021-08-12
      • 2019-01-27
      • 2023-03-27
      • 2017-08-14
      • 1970-01-01
      相关资源
      最近更新 更多