当前位置: 首页 > 知识库问答 >
问题:

使用excel VBA代码将PDF转换为Word文档

荆乐
2023-03-14

每个人。我需要一个VBA代码来将excel数据转换为PDf到word doc,我写了一个宏,将excel打印为pdf,但现在我想从该pdf打印为word,所以简短的版本,Excel到PDF到Word。所有这些都是为了我想保留excel数据的格式

共有2个答案

锺博耘
2023-03-14

我使用相同的基本代码,但我总是收到一条错误消息,说“保存到源文件时出错。它可以被使用。”我有“Adobe Acrobat PRO DC”。我使用参考“Adobe Acrobat 10.0 Type Library”

Option Explicit

Sub convert_pdf_doc()

Dim aApp As Acrobat.AcroApp
Dim av_doc As Acrobat.AcroAVDoc
Dim pdf_doc As Acrobat.AcroPDDoc
Dim jso_obj As Object

Dim sfile As String 'source file
Dim dfile As String 'destination file
Dim ext As String 'my choise of file

ext = "doc"

sfile = "C:\Temp\Test.pdf"
dfile = Replace(sfile, ".pdf", "." & ext, 1)

Set aApp = CreateObject("AcroExch.App")
Set av_doc = CreateObject("AcroExch.AVDoc")

If av_doc.Open(sfile, vbNull) = True Then

    Set pdf_doc = av_doc.GetPDDoc
    Set jso_obj = pdf_doc.GetJSObject
    
    jso_obj.SaveAs dfile, "com.adobe.acrobat." & ext

End If

av_doc.Close False

aApp.Exit
Set aApp = Nothing
Set av_doc = Nothing

End Sub
杨建章
2023-03-14

这将满足您的需要,但您需要安装Adobe Acrobat。我不知道在没有安装Acrobat的情况下有什么方法可以做到这一点。

Option Explicit
Option Private Module

Sub ClearPaths()

    '------------------------------------------
    'Clears the contains of the sheet Paths.

    'By Christos Samaras
    'Date: 30/03/2013
    'http://www.myengineeringworld.net
    '------------------------------------------

    Dim LastRow As Long

    Application.ScreenUpdating = False

    shPaths.Activate

    'Find the last row.
    With shPaths
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    'Clear the contents of the cells.
    If LastRow > 1 Then
        Range(Cells(2, 1), Cells(LastRow, 2)).Value = ""
        Columns("A:B").EntireColumn.AutoFit
    End If

    shPaths.Range("A2").Select
    Application.ScreenUpdating = True

End Sub


Option Explicit
Option Private Module

Sub ExportAllPDFs()

    '----------------------------------------------------------------
    'Converts all the PDF files that their paths are in column A of
    'the worksheet "Convert PDF Files" into a different file format,
    'based on the value in column B (extension).

    'By Christos Samaras
    'Date: 18/07/2013
    'http://www.myengineeringworld.net
    '----------------------------------------------------------------

    Dim LastRow As Long
    Dim i As Integer

    shPaths.Activate

    'Find the last row.
    With shPaths
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    'Check that there are available file paths.
    If LastRow < 2 Then
        shPaths.Range("A2").Select
        MsgBox "There are no file paths to convert!", vbInformation, "File paths missing"
        Exit Sub
    End If

    'Checking for errors before conversion.
    For i = 2 To LastRow

        'Check if the file extensions are not empty.
        If Cells(i, 2).Value = "" Then
            shPaths.Cells(i, 2).Select
            MsgBox "Please select an output format from the dropdown list!", vbCritical, "File paths missing"
            Exit Sub
        End If

        'Check if the file exists.
        If Dir(shPaths.Cells(i, 1).Value) = "" Then
            shPaths.Cells(i, 1).Select
            MsgBox "The file path is not valid!", vbCritical, "File path error"
            Exit Sub
        End If

        'Check if the input file is a PDF file.
        If LCase(Right(shPaths.Cells(i, 1).Value, 3)) <> "pdf" Then
            shPaths.Cells(i, 1).Select
            MsgBox "The file is not a pdf file!", vbCritical, "No pdf file"
            Exit Sub
        End If

    Next i

    'For each cell in the range "A2:A" & last row convert the pdf file
    'into different format according to the "B2:B" & last row value.
    For i = 2 To LastRow
        SavePDFAs Cells(i, 1).Value, Cells(i, 2).Value
    Next i

    'Adjust the two columns.
    Columns("A:B").EntireColumn.AutoFit

    'Inform the user that conversion finished.
    MsgBox "All files were converted successfully!", vbInformation, "Finished"

End Sub

Private Sub SavePDFAs(PDFPath As String, FileExtension As String)

    '---------------------------------------------------------------------------------------
    'Saves a PDF file as other format using Adobe Professional.

    'In order to use the macro you must enable the Acrobat library from VBA editor:
    '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.

    'Alternatively you can find it Tools -> References -> Browse and check for the path
    'C:\Program Files\Adobe\Acrobat xx.0\Acrobat\acrobat.tlb
    'where xx is your Acrobat version (i.e. 9.0 or 10.0 etc.).

    'By Christos Samaras
    'Date: 30/03/2013
    'http://www.myengineeringworld.net
    '---------------------------------------------------------------------------------------

    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

    '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

    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

    End If

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

End Sub
 类似资料:
  • 问题内容: 如何将Word文档转换为PDF,其中文档包含各种内容,例如表格。尝试使用iText时,原始文档看起来与转换后的PDF不同。有没有我可以使用的开源API /库,而不是调用可执行文件? 问题答案: 这是一项艰巨的任务,如果您想要完美的结果(如果不使用Word则不可能),则难度就更大了,因为仅使用纯Java即可为您完成所有操作且都是开源的API数量为零,我相信( 更新:我错了,请参见下文 )

  • 问题内容: 如何使用Java将pdf文件转换为word文件? 而且,它看起来像它一样容易吗? 问题答案: 试试PDFBOX

  • 问题内容: 通过使用如何将文件转换为? 我正在使用以下代码,但无法正常工作,提示错误,我想我导入了错误的类? 问题答案: 得到了解决

  • 我正在尝试使用Apache POI将文档转换为pdf,但生成的pdf文档只包含文本,它没有任何格式,如图像、表格对齐等。

  • 问题内容: 我正在寻找一种使用PHP将Word和Excel文件转换为PDF的方法。 这样做的原因是,我需要能够将各种格式的文件合并到一个文档中。我知道,如果我能够将所有内容转换为PDF,则可以使用PDFMerger(使用fpdf)将PDF合并为一个文件。 我已经能够从其他文件类型/图像创建PDF,但仍受Word Docs困扰。(我想我可以使用已经用于从html代码创建Excel文件的PHPExce

  • 目前我正在尝试将PDF转换为PDF/A。 然而,不知何故,我不知道我是否可以转换色彩空间,有没有办法这样做? 这是我的代码,然而: 色彩空间被添加但是在验证我得到: 对于每个页面/元素,它都经常出现。 我能做点什么来反对它吗?比如转换颜色空间?使用她的图书馆?