我有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"
末次