我有Excel 2010和Acrobat XI Pro。我正尝试使用VBA从具有多个工作表的Excel工作簿创建PDF文件。我需要使用Create PDF函数,而不是ActiveSheet.ExportAsFixedFormat方法,因为Excel工作簿最终有50多个页面,我需要保留指向工作簿中各个部分(即工作表)的内部工作簿超链接。Excel 2010 VBA使用Acrobat创建PDF
我已经能够找到的最好的领导是这个帖子: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"
末次