Link to home
Create AccountLog in
Avatar of rogerdjr
rogerdjrFlag for United States of America

asked on

Printing The first 5 pages of a PDF file using Access VBA

I want to prin the 1st 5 pagese of each of the pdf files in a directory.

Can anybody help with this?
Avatar of als315
als315
Flag of Russian Federation image

I think zone is wrong.
If you have Acrobat reader installed, you should have Adobe PDF Reader ActiveX.
Insert it to your form. Then you can print your file with this code:
Me.AcroPDF5.LoadFile PDFFileFullName
Me.AcroPDF5.printPages 1, 5

AcroPDF5 - name of inserted ActiveX control.
Search for pdf file names you can with this example:
http://filedb.experts-exchange.com/incoming/2011/03_w11/417627/Read-pdf.zip
Avatar of rogerdjr

ASKER

I tried inserting the print pages command (    AcroPDDoc.PrintPages 1, 5) in my code and it hangs with an error message:

"Object doesn't support this property or method"


Public Function AddPageNumbers(ByVal Path As String, ByVal Path1 As String, ByVal filename As String) As String
    Dim ex1 As String
    Dim App As Object, AVDoc As Object, AcroPDDoc As Object, AForm As Object
    Dim Ret As Long
    Dim sString As String * 255

    Set App = CreateObject("Acroexch.app")
    Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
   
    booleanresult = AVDoc.Open(Path, "")
   
    If booleanresult = True Then
        App.Show
        Set AcroPDDoc = AVDoc.GetPDDoc
       
        ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & _
        vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & _
        vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & _
        "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & _
        "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & _
        "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & _
        "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & _
        vbLf & "         fp.value = """
       
        ex1 = ex1 & filename & " " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & _
        " " & Hour(Now) & ":" & Minute(Now)
       
        ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & _
        "         fp.textSize=6; fp.textColor = color.red;  fp.readonly = true;  " & _
        vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "

        AForm.Fields.ExecuteThisJavaScript ex1
    End If
   
    AcroPDDoc.PrintPages 1, 5
    AcroPDDoc.Save 1, Path1

    AcroPDDoc.Close
    AVDoc.Close (True)
    App.Exit
   
    Ret = FindWindowWild(0, "Adobe*")
    Call GetWindowText(Ret, sString, 255)
   
    Ret = FindWindow(vbNullString, sString)
   
    PostMessage Ret, WM_CLOSE, CLng(0), CLng(0)

    Set AcroPDDoc = Nothing
    Set AVDoc = Nothing
    Set App = Nothing
End Function
This code:
Set App = CreateObject("Acroexch.app")
Set AVDoc = CreateObject("AcroExch.AVDoc")
Set AForm = CreateObject("AFormAut.App") 'from AFormAPI

is working only with full Adobe Acrobat installed.
Try to use ActiveX as I've recommended in my comment #35225653
rogerdjr: First things first. Could you please confirm if you have Acrobat Reader Installed or Acrobat Pro?

Sid
I have

C:\Program Files\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe

and

C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe

Can't seem to find the ActiveX as recommended in comment #35225653, or fgure out how to install and use it?
You have both full Acrobat and Acrobar reader installed and you should be able to use your routine.
Read this:
http://bytes.com/topic/access/answers/201801-pdf-only-prints-first-page-when-using-vba-print-acrobat-5-a
and try:
AcroPDDoc.PrintPages 0, 4, 1, True, True
If your document may have less then 5 pages, may be better to count pages before printing
Got it to work - this the code FYI.

Do you have a reference that allows me to specify additional acrobat printing parameters for acrobat docs (e.g. print to fit, rotate,, etc.) - they show-up on the pop-up menu in acrobat and it would be nice to be able to set and reset them with vba.

Thanks

Public Function PrintPdfFile(ByVal PdfPath As String, ByVal PdfFileNm As String, ByVal PdfFirstPageToPrint As Integer, ByVal PdfMaxPagesToPrint As Integer) As String
    Dim PdfPathAndFile As String
    Dim AVDoc As Acrobat.AcroAVDoc
    Dim retcd As Integer
   
    If Right(PdfPath, 1) = "\" Then
        PdfPathAndFile = PdfPath & PdfFileNm
    Else
        PdfPathAndFile = PdfPath & "\" & PdfFileNm
    End If
   
    Set AVDoc = CreateObject("AcroExch.AVDoc")
   
    retcd = AVDoc.Open(PdfPathAndFile, "Title")    ' pathname of pdf file to be opened
   
    If retcd <> -1 Then
'        MsgBox ("Failed to open PDF document " & PdfPathAndFile)
        Exit Function
    End If

    'AVDoc.PrintPages(firstPage, numPages, 2, true, true)
    If PdfMaxPagesToPrint >= 2 Then PdfMaxPagesToPrint = PdfMaxPagesToPrint - 1  'allows for the fact that the first page is page 0
    If PdfFirstPageToPrint >= 1 Then PdfFirstPageToPrint = PdfFirstPageToPrint - 1 'allows for the fact that the first page is page 0
   
    retcd = AVDoc.PrintPages(PdfFirstPageToPrint, PdfMaxPagesToPrint, 0, 1, 1)         ' this prints the number of pages specified by PdfMaxPagesToPrint (i.e. page 0)
   
    AVDoc.Close (1)

End Function
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks

Found this -
Function PrintPagesSilent(nFirstPage As Long, nLastPage As Long, nPSLevel As Long, bBinaryOk As Long, bShrinkToFit As Long) As Boolean
    Member of Acrobat.AcroAVDoc
    Prints a specified range of pages without displaying any dialog box.


Neat tool - I haven't used the object browser before.

Learn something new every day
Thanks

Great support once again