PDA

View Full Version : Automatically print certain pages of pdf



mikeb789
08-01-2011, 12:40 PM
Hi,

I am trying to print only specific pages of a pdf file through vba in excel.

I am able to print the whole file using

Sub printPDF()
Shell "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe /p /h" & "FileName.pdf", vbNormalFocus
End Sub

however the pdf is about 20 pages and I only need the first three. Any help would be greatly appreciated.

Thanks,
Mike

mohanvijay
08-01-2011, 09:03 PM
You need to reference acrobat library and try the code below



Dim pri_err As Integer
Dim ad_app As New Acrobat.AcroApp
Dim ad_ac_doc As New Acrobat.AcroAVDoc
Dim ad_pd_doc As Acrobat.AcroPDDoc

ad_ac_doc.Open FilePath
pri_err = ad_ac_doc.PrintPagesSilent(0,2, 2, 0, 0)
ad_ac_doc.Close 1
ad_pd_doc.Close

Set ad_pd_doc = Nothing
Set ad_ac_doc = Nothing
Set ad_app = Nothing

mikeb789
08-02-2011, 06:21 AM
Thanks for the help. I tried the code and at the .open in ad_ac_doc.Open "filename.pdf"
it gives me a compile error: Argument not optional. Any Ideas?

Thanks again,
Mike

Kenneth Hobs
08-02-2011, 06:52 AM
If you had set the acrobat.tlb, intellisense would tell you that the second parameter in Open() is szTempTitle. I am not sure what that means. I just used Len(). I have not tested this yet.

Sub Test_PrintPDFByPages()
PrintPDFByPages "x:\pdf\test.pdf", lngLastPage:=2
End Sub

'Add reference, Adobe Acrobat 9.0 Type Library, acrobat/tlb
Sub PrintPDFByPages(sFilePath As String, Optional lngFirstPage As Long = 0, Optional lngLastPage As Long, _
Optional lngPSLevel = 2, Optional lngBinaryOK As Long = 0, Optional lngShrinkToFit As Long = 0)
Dim pri_err As Boolean
Dim ad_app As New Acrobat.AcroApp
Dim ad_ac_doc As New Acrobat.AcroAVDoc
Dim ad_pd_doc As Acrobat.AcroPDDoc
Dim tf As Boolean
tf = ad_ac_doc.Open(sFilePath, Len(sFilePath))
pri_err = ad_ac_doc.PrintPagesSilent(lngFirstPage, lngLastPage, lngPSLevel, lngBinaryOK, lngShrinkToFit)
ad_ac_doc.Close 1
ad_pd_doc.Close

Set ad_pd_doc = Nothing
Set ad_ac_doc = Nothing
Set ad_app = Nothing
End Sub

mohanvijay
08-02-2011, 08:26 PM
Change like below


ad_ac_doc.Open FilePath,""