Excel 2010 VBA使用Acrobat创建PDF

时间:2017-08-15 12:12:52

标签: excel-vba pdf-generation acrobat vba excel

我有Excel 2010和Acrobat XI Pro。我正在尝试使用VBA从具有多个工作表的Excel工作簿创建PDF文件。我需要使用Create PDF函数而不是ActiveSheet.ExportAsFixedFormat方法,因为Excel工作簿最终是50多页,我需要保留指向工作簿中各个部分(即工作表)的内部工作簿超链接。

我能找到的最佳线索是这篇文章:http://stackoverflow.com/questions/37551957/using-vba-how-do-i-call-up-the-adobe-create-pdf-function 这篇文章:https://forums.adobe.com/thread/853854

我可以成功创建一个空PDF并从另一个源PDF插入页面。但是,我无法在空PDF中插入工作表。 (我也尝试过以PDF开头的方法,这个方法已经有一页同样的结果)。看来我无法将工作表“对象”分配给PDF源。我一直在搜索谷歌没有运气......这是我的VBA代码,我非常感谢任何反馈。

Sub ExportWithAcrobat4()
Dim strFileName As String
Dim objPDDocNew As Object
Dim objPDDoc As Object
Dim nInsertPageAfter As Integer
Dim nStartPage As Integer
Dim nNumPages As Integer
Dim bBookmarks As Integer
'**********************************

strFileName = "E:\Documents\WORK\Document Control\test1.pdf"

Set objPDDocNew = CreateObject("AcroExch.PDDoc")
 If objPDDocNew.Create() = False Then
    MsgBox "Did not create Combined PDF file.", vbCritical + vbOKOnly, "File Not Created"
    Exit Sub
End If

 Set objPDDoc = CreateObject("AcroExch.PDDoc")
If objPDDoc.Open(strFileName) = False Then
    MsgBox "Could not open " & strFileName
    Return
End If
nNumPages = objPDDoc.GetNumPages

'--- Add pages to new file ---
'from Acrobat SDK:
'VARIANT_BOOL InsertPages(long nInsertPageAfter, LPDISPATCH iPDDocSource, long nStartPage, long nNumPages, long bBookmarks);
'nInsertPageAfter: The page in the current document after which pages from the source document are inserted. The first page in a PDDoc object is page 0.
'iPDDocSource: The LPDISPATCH for the AcroExch.PDDoc containing the pages to insert. iPDDocSource contains the instance variable m_lpDispatch, which contains the LPDISPATCH.
'nStartPage: The first page in iPDDocSource to be inserted into the current document.
'nNumPages: The number of pages to be inserted.
'bBookmarks: If a positive number, bookmarks are copied from the source document. If 0, they are not.
nInsertPageAfter = -1
nStartPage = 0
bBookmarks = 1

'++++++++++++ EXCEL STUFF ++++++++++++
ThisWorkbook.Sheets("Sheet1").Select
'need to set the worksheet = objPDDoc somehow
'+++++++++++++++++++++++++++++++++++++

If objPDDocNew.InsertPages(nInsertPageAfter, objPDDoc, nStartPage, nNumPages, bBookmarks) = False Then 'this works with a pdf file as source
    MsgBox "Pages did not insert"
    Exit Sub
End If

'save the new file
objPDDocNew.Save 1, "E:\Documents\WORK\Document Control\testResult.pdf"
objPDDocNew.Close
MsgBox "done"

End Sub

1 个答案:

答案 0 :(得分:0)

PDFMaker加载项没有受支持的API。您可能会发现一些人们尝试过的代码,但是没有官方文档,而且可能随更新而改变。