none
VBA to open and print specific pages in a PDF file RRS feed

  • Question

  • VBA beginner and very little programming experience and asking for help please.  I've searched for hours and finding everything except what I need online.

    I need to print from both Excel and also pages from a pdf file in a specific order.

    I have figured out the VBA to print Excel worksheets in the order I need them but not from a PDF file.

    Example: Print 1st tab in Excel, then print page 9 & 10 from PDF , then 2nd tab in Excel, then page 11 from PDF, etc.

    I have figured out how to open the PDF file but not print it.  (I haven't found the command to close PDF to continue with the VBA script either)

    It is not important if I open the PDF file or not.  All I need to know is how to print a page or a range of pages from the PDF file.

    Example script I what I have so far:

    Public Sub Open_PDF_At_Page_No()

        Dim sPDFfile As String
        Dim sAdobeCommand As String
           
        'Adobe Reader
        Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"
       
        'PDF file
        sPDFfile = "C:\path to pdf file\pdf filename.pdf"
       
        'Adobe command
        sAdobeCommand = " /a ""page=10=Open Actions"" "
       
        ' This opens Adobe Reader
        ' Then opens the PDF file to page 10
        ' ***** Still need to figure out how to print certain page numbers *****
       
        ' this works to open the PDF file to page 10
        Shell cAdobeReaderExe & sAdobeCommand & Chr(34) & sPDFfile & Chr(34), vbNormal 'vbMinimizedNoFocus  'open minimized ', vbNormal
        
        'Docmd.PrintOut (- Don't work)
        'ShellExecute 0, "print", sAdobeCommand, "", "", 0 (- Don't work)
       
        'Debug.Print " /a ""page=10"" ", vbNormal (- Don't work)
       
    End Sub

    Sunday, May 17, 2015 10:25 PM

Answers

  • Hi Rick,

    Thanks for the detail information.

    If you want to print the specific pages,  base on my understanding we can automate application which open PDF files. And from the code above, you were using Adobe reader.

    To get more effective response, I would suggest that you get support from support from Official site from Adobe to see whether it support automation.

    If you have any other problem with Office developing, please feel free to ask here.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 25, 2015 7:59 AM
    Moderator

All replies

  • Hi,

    This is the forum to discuss questions and feedback for Microsoft Excel, I'll move your question to the MSDN forum for Excel

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=exceldev&filter=alltypes&sort=lastpostdesc

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Thanks

    George Zhao
    Forum Support
    ________________________________________
    Come back and mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback on our support, please click "tnfsl@microsoft.com"

    Monday, May 18, 2015 2:08 AM
  • Hi,

    Accorrding to the description, you want to print the specific pages in spreadsheets and PDF file.

    >>I have figured out the VBA to print Excel worksheets in the order I need them but not from a PDF file.<<

    Based on my understanding, we can use Excel object model to automate printing worksheets and use Worksheet.Print method to set the specific pages we wanted print.

    Since Word 2013 support to view/edit PDF files, if you are using Word 2013, we can also use it to print PDF files. And to achieve the goal we can use Document.PrintOut and here is an sample that print the first three pages of the document in the active window:

    ActiveDocument.ActiveWindow.PrintOut _ 
     Range:=wdPrintFromTo, From:="1", To:="3"
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 19, 2015 5:19 AM
    Moderator
  • Hi Fei,

    Thank you for your response.  Unfortunately, this is probably not an option.  I believe the person I am doing this for has Excel 2007 or 2010.

    I tried using what you added above but got an error message.

    In the code I can open the PDF to a certain page (page 10 is what the code says) but I'm unable to figure out how to print the PDF page 10.

      I also need to figure out how to close the PDF using code.

    Any ideas?

    Thanks,

    Rick


    Thank you, Rick

    Sunday, May 24, 2015 6:27 PM
  • Hi Rick,

    Thanks for the detail information.

    If you want to print the specific pages,  base on my understanding we can automate application which open PDF files. And from the code above, you were using Adobe reader.

    To get more effective response, I would suggest that you get support from support from Official site from Adobe to see whether it support automation.

    If you have any other problem with Office developing, please feel free to ask here.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 25, 2015 7:59 AM
    Moderator