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

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

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?

Thanks,
Kelvin
0
Kelvin4
Asked:
Kelvin4
  • 2
1 Solution
 
MacroShadowCommented:
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
    PDDocSource.Close
    PDDocTarget.Close
    
    ' 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.
0
 
Kelvin4Author Commented:
Just the information I sought - many thanks!
Kelvin
0
 
MacroShadowCommented:
My pleasure.
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.

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