Learn how to a build a cloud-first strategyRegister Now


Can vba automation be used to view a subset of pages in the same PDF document (ie to hide irrelevant blocks of pages)?

Posted on 2014-09-01
Medium Priority
Last Modified: 2014-09-01
My excel 2010 piece uses vba to open a PDF file and display the page with a defined page number.

Is it possible to HIDE some pages in a document of (say) 100 pages?
For example, if I wish to display pages 40-50, and could HIDE pages 1-39 and 51-100, then the reader could not scroll onto irrelevant pages. However, for another consultation, I would wish to pick another block of pages to read, and hence a different block of pages to hide.

Can this be done, perhaps through Layers? If so, would those layers be definable dynamically, or would they have to be created when the PDF file is created?

Question by:Kelvin4
  • 2
LVL 28

Accepted Solution

MacroShadow earned 2000 total points
ID: 40296764
The only option as I see it is automating Acrobat Professional (not reader) thru VBA to extract the pages you need to a new pdf document.
Sub ExtractPDFPages(strSourceFullPath As String, strDestinationFullPath As String, iStartPage As Integer, iNumPages As Integer)

    Dim PDDocSource As Object, PDDocTarget As Object
    Set PDDocSource = CreateObject("AcroExch.PDDoc")
    Set PDDocTarget = CreateObject("AcroExch.PDDoc")
    ' Create a new PDDoc
    If PDDocTarget.Create <> True Then
        MsgBox "Unable to create a new PDF"
        Exit Sub
    End If
    ' Open the PDF source file (the file we are going to take pages from)
    If PDDocSource.Open(strSourceFullPath) <> True Then
        MsgBox "Unable to open the source PDF"
        Exit Sub
    End If
    ' Set the page range you wish to extract
    ' Don't forget that this is zero based
    iStartPage = 0

    ' Set the number of pages you wish to extract
    iNumPages = 2
    ' Insert the pages from the source PDF file to the target PDF
    If PDDocTarget.InsertPages(-1, PDDocSource, iStartPage, iNumPages, False) <> True Then
        MsgBox "Unable to insert the source pages"
        Exit Sub
    End If
    ' Save the new file
    If PDDocTarget.Save(&H1, strDestinationFullPath) <> True Then
        MsgBox "Unable to save the pdf"
        Exit Sub
    End If
    'Close the PDF files
    ' Clean up
    Set PDDocSource = Nothing
    Set PDDocTarget = Nothing
End Sub

Open in new window

Sample usage:
ExtractPDFPages "C:\Expert-Exchange Demos\Source.pdf", "C:\Expert-Exchange Demos\Destination.pdf", 40, 11

Open in new window

Will extract pages 40-50 to a new pdf.

Author Closing Comment

ID: 40297301
Just the information I sought - many thanks!
LVL 28

Expert Comment

ID: 40297302
My pleasure.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question