VBA - Tips |
Summary & Introduction |
Adobe Acrobat Professional |
Adobe Acrobat |
---|
VBA Control of Adobe Acrobat Professional
Adobe Acrobat Professional is more automatically controlled by JavaScript, another computer language, but Adobe has provided Inter-Application Communication (IAC) that allows VBA to interface with JavaScript.
combine multiple Acrobat files into one,
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
acroAVDoc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
acroPDDoc
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Opening Acrobat Dim acroApp As Acrobat.acroApp Dim avDoc As Acrobat.acroAVDoc Set acroApp = CreateObject("AcroExch.App") Set avDoc = CreateObject("AcroExch.AVDoc") 'avDoc.Open FileLocation, TitleToDisplay avDoc.Open "D:\Users\User\Desktop\Test.pdf", "PDF Title" avApp.Show 'The next 2 lines to refer to the PDDoc from an AVDoc Dim pdDoc As Acrobat.AcroPDDoc Set pdDoc = acroDoc.GetPDDoc Set pdDoc = Nothing Set avDoc = Nothing Set acroApp = Nothing
Hidden PDF Dim pdDoc As Acrobat.AcroPDDoc 'or .CAcroPDDoc Set pdDoc = CreateObject("AcroExch.PDDoc") pdDoc.Open "D:\Users\User\Desktop\Test.pdf" 'The acrobat is now open but invisible. 'Create an AVDoc from a PDDoc if you wish to make the pdf visible. 'OpenAVDoc and give the pdf a title that is not necessarily the name of the file. pdDoc.OpenAVDoc "Title" 'When you manually close a visible acrobat the acrobat application will close. 'However if you did not make acrobat visible then you can not easily close acrobat. 'You should use the following code to close the hidden acrobat application. 'pdDoc.Close Set pdDoc = Nothing Set acroApp = Nothing
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr Public Const WM_CLOSE = &H10 Sub CloseAcrobat() Dim hwnd As LongPtr hwnd = FindWindow("AcrobatSDIWindow", vbNullString) 'Close down Notepad. SendMessage hwnd, WM_CLOSE, 0&, 0& End Sub
Determine the Number of Pages in an Open PDF Dim acroApp As acroApp Dim avDocAs AcroAVDoc Dim pdDoc As AcroPDDoc Dim intNoPages As Integer 'Create an Acrobat object. Set acroApp = CreateObject("AcroExch.App") 'Get the already opened PDF file. Set avDoc = acroApp.GetActiveDoc 'Get the pdDoc of the avDoc. Set pdDoc = avDoc.GetPDDoc intNoPages = pdDoc.GetNumPages Set pdDoc = Nothing Set avDoc = Nothing Dim acroApp As Nothing
Dim acroApp As acroApp Dim avDoc As AcroAVDoc Dim pdDoc As AcroPDDoc Dim bnSuccess As Boolean 'Create an Acrobat object. Set acroApp = CreateObject("AcroExch.App") 'Get the already opened PDF file. Set avDoc = acroApp.GetActiveDoc Set pdDoc = avDoc.GetPDDoc 'ie Move to page 4, page 2. bnSuccess = pdDoc.MovePage(3, 1) 'or 'pdDoc.MovePage 3, 1 Set pdDoc = Nothing Set avDoc = Nothing Set acroApp = Nothing
Dim pdDoc As AcroPDDoc Dim acroPage As AcroPDPage Dim intPageCounter As Integer Set pdDoc = CreateObject("AcroExch.PDDoc") 'The path is case sensitive. pdDoc.Open ("D:\Users\User\Desktop\Test.pdf") 'Get each page. For intPageCounter = 0 To pdDoc.GetNumPages - 1 Set acroPage = pdDoc.AcquirePage(lngPageCounter) 'Some code goes here. Set acroPage = Nothing Next intPageCounter pdDoc.Close Set pdDoc = Nothing
Dim acroApp As Acrobat.acroApp Dim pdDoc As Acrobat.AcroPDDoc Dim acroPage As AcroPDPage Set acroApp = New acroApp acroApp.Show '.Maximize 'If the argument is a positive number, the Acrobat application is maximized. 'If the argument is 0 the Acrobat application is returned to its normal state. acroApp.Maximize 1 Set pdDoc = New AcroPDDoc pdDoc.Open "D:\Users\Graeme\Desktop\Test.pdf" pdDoc.OpenAVDoc "Working Doc" 'Set page object Set acroPage = pdDoc.AcquirePage(0) acroPage.SetRotate 90 'Other Methods & Properties are; 'acroPage.AddAnnot 'acroPage.AddNewAnnot 'acroPage.CopyToClipboard 'acroPage.CreatePageHilite 'acroPage.CreateWordHilite 'acroPage.CropPage 'acroPage.Draw 'acroPage.DrawEx 'acroPage.GetAnnot 'acroPage.GetAnnotIndex 'acroPage.GetDoc 'acroPage.GetNumAnnots 'acroPage.GetNumber 'acroPage.GetRotate(Degrees clockwise) 'acroPage.GetSize 'acroPage.RemoveAnnot 'acroPage.SetRotate Set acroPage = Nothing Set pdDoc = Nothing Set acroApp = Nothing
Dim acroApp As acroApp Dim pdDoc As AcroPDDoc Dim intNoPages As Integer Dim intInsertPageNo As Integer Set acroApp = CreateObject("AcroExch.App") Set pdDoc = CreateObject("AcroExch.PDDoc") 'pdDoc.Open (Case sensitive full path to pdf) pdDoc.Open ("C:\Users\User\Desktop\Test.pdf") 'Determine the total number of pages in the original document. intNoPages = pdDoc.GetNumPages() '*************************************************************** 'Important: Acrobat numbers pages from zero being the first page. 'This makes it confusing. '*************************************************************** 'Copy a page from the back of pdDoc towards the front of pdDoc. 'intInsertPageNo does not just move pages but copies them as well. 'InsertPages(nInsertPageAfter As Long, iPDDocSource As Object, lngStartPage As Long, lnNoPages As Long, lngInsertBookmarks As Long) As Boolean 'If nInsertPageAfter = -1 then place copy before 1st page. 'If nInsertPageAfter = 0 then place copy after 1st page. 'If nInsertPageAfter = 1 then place copy after 2nd page. 'lngInsertBookmarks = 0 ie False do not merge Bookmarks. For intInsertPageNo = 1 To intNoPages pdDoc.InsertPages intInsertPageNo - 2, pdDoc, intNoPages - 1, 1, 0 Next intInsertPageNo 'Delete the original pages. 'DeletePages(nStartPage As Long, nEndPage As Long) 'For some reason acrobat remembers the page numbers of the original sheets, not their new position. pdDoc.DeletePages intNoPages, intNoPages * 2 - 1 'Save the pdf. 'Insert your path and file name here for the new pdf. If pdDoc.Save(PDSaveFull, "C:\Users\User\Desktop\Reversed.pdf") = False Then MsgBox "Cannot save the modified pdf." End If pdDoc.Close acroApp.Exit Set pdDoc = Nothing Set acroApp = Nothing
Dim acroApp As acroApp Dim pdDoc As AcroPDDoc Dim intNoPages As Integer Dim intInsertPageNo As Integer Set acroApp = CreateObject("AcroExch.App") Set pdDoc = CreateObject("AcroExch.PDDoc") 'The pages in pdDoc will be placed in reverse order. pdDoc.Open ("C:\Users\User\Desktop\Test.pdf") 'Determine the total number of pages in the original document. intNoPages = pdDoc.GetNumPages() intInsertPageNo = 0 '*************************************************************** 'Important: Acrobat numbers pages from zero being the first page. 'This makes it confusing. '*************************************************************** 'InsertPages(nInsertPageAfter As Long, iPDDocSource As Object, _ 'lngStartPage As Long, lnNoPages As Long, lngInsertBookmarks As Long) As Boolean 'lngInsertBookmarks = 0 ie False do not merge Bookmarks. 'Insert the 1st page to the 2nd page. ie Move every 2nd page back. For intInsertPageNo = 0 To intNoPages Step 2 pdDoc.InsertPages intInsertPageNo + 1, pdDoc, intInsertPageNo, 1, 0 'InsertPages moves a copy of a page but does not delete the original page so it must be deleted. 'DeletePages(nStartPage As Long, nEndPage As Long) pdDoc.DeletePages intInsertPageNo, intInsertPageNo Next intInsertPageNo 'Save the pdf. If you want you can save the pdf to another name so as not to alter the original file. If pdDoc.Save(PDSaveFull, "C:\Users\User\Desktop\Test.pdf") = False Then MsgBox "Cannot save the modified document" End If pdDoc.Close acroApp.Exit Set pdDoc = Nothing Set acroApp = Nothing |
Issue date 2021_09_25