• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1980
  • Last Modified:

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?
0
rogerdjr
Asked:
rogerdjr
  • 5
  • 4
1 Solution
 
als315Commented:
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
0
 
rogerdjrAuthor Commented:
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
0
 
als315Commented:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
SiddharthRoutCommented:
rogerdjr: First things first. Could you please confirm if you have Acrobat Reader Installed or Acrobat Pro?

Sid
0
 
rogerdjrAuthor Commented:
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?
0
 
als315Commented:
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
0
 
rogerdjrAuthor Commented:
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
0
 
als315Commented:
You can use Object Browse in VBA editor. Select proper Acrobat library and you can see available functions
0
 
rogerdjrAuthor Commented:
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
0
 
rogerdjrAuthor Commented:
Thanks

Great support once again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now