Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Specific Page from PDF File from Excel

Status
Not open for further replies.

hext2003

Technical User
Oct 9, 2006
119
US
I have code like this in a module -


Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

Then I call this with a button click:

Private Sub RePrint()
ShellExecute Me.hwnd, "open", "C:\MyFile.pdf", vbNullString, "C:\", SW_SHOWNORMAL
End Sub

This opens my pdf file just fine. What I want it to do is Print Page "RePrintRecNum" where "RePrintRecNum" is the page to be printed. this document has 15,000 pages and I only need 1.

Then I want it to close the PDF file.

Any ideas?

 
The following vb6 code does not answer your question but contains most of the necessary refrences; it may give you a few ideas;
Code:
Public Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long

Public Function DisplayPdfReference&(Filename$, PageNumber%)
    
    'Display the required Page in the default .pdf handler, typically Acrobat reader
    'AdobeInstance& is Global for the calling App, I guess it could be a Static local
    'goto page number functionality may only work with Adobe reader v8.nnn
    
    Dim lStatus&
    Dim sAcroPath$, q$
    
    q$ = """"
    
    If AdobeInstance Then
        'the pdf handler may be running already
        On Error Resume Next
        AppActivate AdobeInstance
        If Err Then AdobeInstance = 0   'no it is'nt
    End If
    
    If AdobeInstance Then
        ' it is still running
        Sendkeys "%V", True
        Sendkeys "g", True
        Sendkeys "p", True
        Sendkeys CStr(PageNumber%), True
        Sendkeys "{ENTER}", True
    Else
        'Use the FindExecutable API function to grab the path to our PDF handler.
        'This should be Acrobat Reader or Acrobat, but it might be something else.
        'ref [URL unfurl="true"]http://www.vb-helper.com/howto_print_pdf.html[/URL]
        sAcroPath = String(128, 32)
        If FindExecutable(Filename$, vbNullString, sAcroPath) <= 32 Then
            MsgBox "Adobe Acrobat, Adobe Acrobat reader, or similar software could not be found on this computer.", vbCritical, "Cannot display pdf because ..."
        Else
            sAcroPath$ = Left$(sAcroPath$, InStr(sAcroPath$, Chr$(0)) - 1)
            'ref [URL unfurl="true"]http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf[/URL]
            '   [URL unfurl="true"]http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/intro_to_sdk/DeveloperFAQ.pdf[/URL]
            On Error Resume Next
            AdobeInstance& = Shell(sAcroPath$ & " /n /A " & q$ & "page=" & PageNumber% & q$ & " " & q$ & Filename$ & q$, vbNormalFocus)
            If Err Then MsgBox "Unable to start a default pdf display program": AdobeInstance& = 0
        End If
        DisplayPdfReference& = AdobeInstance&
    End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top