Solved

VBA code to Open PDF file and print page range (just page 1)

Posted on 2007-12-07
22
9,109 Views
Last Modified: 2016-08-31
Hi,
I am receiving zips of 100+ PDF files in a folder and I need to open each PDF and print just page 1 from each file.  I'm fine w/ the code to get the pathname for each individual file in the folder and loop thru all 100+ PDF filenames.  

What I need help with is how to open a single one of the PDF files and print only page 1 to the default windows printer and then close the PDF file.  

Hope someone can help.

Thanks in advance
Debbie
0
Comment
Question by:debdba
  • 14
  • 7
22 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20427166
I am using acropdf. this should help.

Public filename As String
Private Sub cmdprint_Click()

If File1.ListCount = 0 Then MsgBox "Nothing to print ": Exit Sub
For i = 0 To File1.ListCount - 1
filename = File1.List(i)
Screen.MousePointer = vbHourglass
AcroPDF1.Visible = False
AcroPDF1.LoadFile filename
AcroPDF1.Visible = True
Screen.MousePointer = vbNormal
Me.Refresh
DoEvents
AcroPDF1.printPages 1, 1
Next
End Sub

Private Sub cmdshow_Click()
AcroPDF1.Visible = False
End Sub
Private Sub Dir1_Change()
Label1.Caption = Dir1.Path
File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub File1_Click()
filename = Replace(Dir1.Path & "\" & File1.filename, "\\", "\", 1, 1, vbTextCompare)
End Sub


Private Sub Form_Load()
AcroPDF1.Visible = False
End Sub


0
 

Author Comment

by:debdba
ID: 20427547
Thanks for responding.  I'll have to spend a little time on this.  But in the meantime, what's Acropdf and is it free?
0
 

Author Comment

by:debdba
ID: 20427679
Hmmm...  It looks like this doesn't really PRINT, it's creating  a PDF file.  I'm asking something different.

I already have hundreds of PDF files and I want to open each one and literally print, on paper, just the first page of each.
Debbie
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20429102
I am sorry there was issue in this procedure. This will display the print dialog box with all the settings.
You can use the API to find the print window after it is displayed and send key to print.

Private Sub cmdprint_Click()
If File1.ListCount = 0 Then MsgBox "Nothing to print ": Exit Sub
For i = 0 To File1.ListCount - 1
filename = Replace(Dir1.Path & "\" & File1.List(i), "\\", "\", 1, 1, vbTextCompare)
Screen.MousePointer = vbHourglass
AcroPDF1.Visible = False
AcroPDF1.LoadFile filename
AcroPDF1.Visible = True
Screen.MousePointer = vbNormal
AcroPDF1.printPages 1, 1
AcroPDF1.printWithDialog
Next
End Sub
0
 

Author Comment

by:debdba
ID: 20429284
I don't want to have to deal w/ a dialog box, though.  I'm looking for a way to print the 1st page only of 100 pdf files to the default windows printer with no intervention on my part.

I probably need to shell out to some kind of command line, but can't find any such thing for printing a pdf file and selecting a page range.
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20429294
This you can achive by handling sendkeys code from vb.
0
 

Author Comment

by:debdba
ID: 20429419
How, exactly?
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20429603
This should help. This works in my computer. The dialog box is automatically closed.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim ie_hWnd As Long
Dim sAppName As String
Public filename As String

Private Sub cmdprint_Click()

If File1.ListCount = 0 Then MsgBox "Nothing to print ": Exit Sub
For i = 0 To File1.ListCount - 1
If i > 0 Then Sleep 5000
ie_hWnd = 0
filename = Replace(Dir1.Path & "\" & File1.List(i), "\\", "\", 1, 1, vbTextCompare)
Screen.MousePointer = vbHourglass
AcroPDF1.Visible = False
AcroPDF1.LoadFile filename
AcroPDF1.Visible = True
Screen.MousePointer = vbNormal
AcroPDF1.printPages 1, 1
AcroPDF1.printWithDialog
sAppName = "Print"
Dim k As Integer
k = 0

xx:

ie_hWnd = FindWindow(vbNullString, sAppName)
If ie_hWnd = 0 Then
Debug.Print k
k = k + 1
If k = 10000 Then Exit Sub
    GoTo xx
    Else
    sAppName = "Print"
    AppActivate sAppName
VbSendKeys "{ENTER}"

    'Exit Sub
End If
DoEvents
On Error GoTo yy
   


Next
Exit Sub
yy:
If Err.Number = 5 Then GoTo xx
End Sub

Private Sub cmdshow_Click()
AcroPDF1.Visible = False
End Sub
Private Sub Dir1_Change()
Label1.Caption = Dir1.Path
File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub File1_Click()
filename = Replace(Dir1.Path & "\" & File1.filename, "\\", "\", 1, 1, vbTextCompare)
End Sub


Private Sub Form_Load()
AcroPDF1.Visible = False
End Sub


0
 

Author Comment

by:debdba
ID: 20431508
Can you tell me how you got AcroPDF?  Is that part of Adobe Acrobat, or something you have to buy, or what?
Thanks.
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20440842
0
 

Author Comment

by:debdba
ID: 20540515
Hi Shasunder:
I'm so sorry this is still open and I really haven't forgotten you.  I just got busy w/ the Christmas rush and haven't gotten back to the programming problem.  I'll try to get back to this next week.
Debbie
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:debdba
ID: 20631374
Hi Shasunder:
I'm so sorry this is still open and I really need this assistance but haven't had time to get back on this project.   I'll try to get back to this weekend.  I want to leave it open so I can ask you questions if I get stuck.
Thanks,
Debbie
0
 

Author Comment

by:debdba
ID: 20696300
Alright, I've been working on this and I downloaded ACROPDF and can't see anywhere anything about reading IN pdf files.  I have two questions:

1.  You use this line of code:
             AcroPDF1.LoadFile filename
             Can filename = "C:\junk.pdf"???  CAN YOU LOAD A PDF FILE?
2.  Which lines in your code actually cause a piece of paper to come out on the real printer?

I'm just still not sure that you're showing me how to READ IN A PDF FILE and PRINT OUT ONE PAGE of the pdf file on a physical printer with paper.

Thanks,
Debbie
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20696933
This line will set the acrobat printer to print only the first page

AcroPDF1.printPages 1, 1

Also to your question,  Can filename = "C:\junk.pdf"???  CAN YOU LOAD A PDF FILE?

Its left to the acrobat PDF reader. it will take care either the loaded file is in pdf format or not.The line AcroPDF1.LoadFile filename will do it for you
0
 

Author Comment

by:debdba
ID: 20697737
Hi shasunder,
OK, so I CAN read in a PDF file.  That's good.

"This line will set the acrobat printer to print only the first page"
I guess I'm still unclear on this- doesn't the "acrobat printer" / printPages method  put out a one page PDF file and not a sheet of paper on a physical printer?
Thanks for the reply,
Debbie
0
 

Author Comment

by:debdba
ID: 20698288
And one more question:
How do you add the reference you need to be able to use the AcroPDF object
and
How do you declare/dim the AcroPDF1 object?
0
 

Author Comment

by:debdba
ID: 20698311
or does AcroPDF give you a new tool in the toolbox to drop on your form and that's where AcroPDF1 comes from?
0
 

Accepted Solution

by:
debdba earned 0 total points
ID: 20700033
Well, this isn't using AcroPDF, but HERE'S WHAT DOES WORK.  Seems a whole lot easier to me.  I would still like to learn how to get access to the AcroPDF object, though.

First, add a reference to Acrobat in the VB Editor by going to Tools...References...check Acrobat.  This gives you access to the objects needed.

Dim AVDoc As Acrobat.AcroAVDoc
Dim retcd As Integer
Set AVDoc = CreateObject("AcroExch.AVDoc")
retcd = AVDoc.Open("C:\junk.pdf", "Title")    ' pathname of pdf file to be opened
    If retcd <> -1 Then
        MsgBox ("Failed to open PDF document " & "C:\junk.pdf")
        Exit Sub
    End If
retcd = AVDoc.PrintPages(0, 0, 0, 1, 1)       ' this prints just page 1 (i.e. page 0)
AVDoc.Close (1)
0
 

Author Comment

by:debdba
ID: 20793760
Hi Shasunder,
I'm being asked to close out this question, but I haven't heard back from you on the question I asked.  I figured out a way that works for me (previous post), but I'd really like to understand how to get to the AcroPDF object you reference so I can use it.

Can you tell me how you declared/defined the AcroPDF1 object?  Was it a tool in your toolbox that you dropped onto your form?  Or do you bring in some class library that has an AcroPDF object w/ properties and methods you can use?  And if so, where did you get the class library from?

It looks like it could be a part of the Developer's Toolkit you can get from Adobe for free, rather than buy it at the link you gave me.  But I can't figure out how to create your AcroPDF1 object in trying to wade thru the documentation that comes w/ the Adobe Developers Toolkit.  Very confusing.
Thanks,
Debbie
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20936702
Sorry debdba,

I actually didnot see your post. Due to some reasons I did not get any mail regarding this. I am really sorry for what had happened.
0
 

Author Comment

by:debdba
ID: 20954581
Hi shasunder,
It's good to hear back from you.  I'd still love to understand your solution and get an answer to my questions in the last comment.  Do you want me to reopen the question (as a new question)?  It might help others and would give you the points.
Thanks,
Debbie
0
 

Expert Comment

by:Mike R
ID: 41778305
Hi All,
I have a large PDF file. I am trying to print a few select pages to a new PDF file using VBA. It seems to be working except that Adobe is prompting me to specify the name of the new file. I want the naming of the new file to be included in the program. I cannot figure out the appropriate syntax to do this. Any advice?
Thanks,
Mike


Sub Print_PDFs2()

Dim AVDoc As Acrobat.AcroAVDoc
Dim retcd As Integer
Set AVDoc = CreateObject("AcroExch.AVDoc")
retcd = AVDoc.Open("C:\Users\test.pdf", "test.pdf")

Application.Dialogs(xlDialogPrinterSetup).Show  'AT THIS POINT I SELECT ADOBE PDF AS THE PRINTER
retcd = AVDoc.PrintPages(0, 2, 0, 1, 1)

AVDoc.Close (1)
End Sub
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In this first video of the three-part Xpdf series, we introduce and describe Xpdf, a library containing nine command line utilities that perform various functions on PDF files. We show where the library is located and how to download it, discuss its…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now