For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > April 2005 > Print several .pdf documents









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Print several .pdf documents
Mary

2005-04-27, 3:55 pm

Hi!

I'm trying to do a Visual Basic program that allows me print all .pdf
documents inside a folder. If there is only one .pdf document inside the
folder my program run propertly, but unfortunately when there are several
..pdf documents it only print the last 2 ones. I supose that it happens
because I send too much information to the printer, but Is there any way of
sending data to the printer and sleeping the program until the printer
finished printing?

My code is this:

temp_path = directory & "\*.pdf" 'Folder where the .pdf are
str_dir = Dir(temp_path, vbNormal)

Do While str_dir <> ""
FilePDF = directoriy & "\" & str_dir
Pdf1.LoadFile ""
DoEvents
Pdf1.LoadFile FilePDF
Pdf1.printAll

str_dir = ""
str_dir = Dir$()

Loop

Thanks a lot

Mary
Karl E. Peterson

2005-04-27, 3:55 pm

Mary wrote:
> I'm trying to do a Visual Basic program that allows me print all .pdf
> documents inside a folder. If there is only one .pdf document inside
> the folder my program run propertly, but unfortunately when there are
> several .pdf documents it only print the last 2 ones. I supose that
> it happens because I send too much information to the printer, but Is
> there any way of sending data to the printer and sleeping the program
> until the printer finished printing?
>
> My code is this:
>
> temp_path = directory & "\*.pdf" 'Folder where the .pdf are
> str_dir = Dir(temp_path, vbNormal)
>
> Do While str_dir <> ""
> FilePDF = directoriy & "\" & str_dir
> Pdf1.LoadFile ""
> DoEvents
> Pdf1.LoadFile FilePDF
> Pdf1.printAll
>
> str_dir = ""
> str_dir = Dir$()
>
> Loop


Dunno what that Pdf1 object you have there may be, but it sounds like you've found a
limitation with it. Maybe it has some sort of "wait" functionality? Another choice
would be to pass each document to a function like this:

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

Public Function PrintDoc(ByVal DocFile As String) As Long
' Uses the "print" verb as defined in the registry
PrintDoc = ShellExecute(0&, "print", DocFile, vbNullString, vbNullString,
vbNormalFocus)
End Function

Later... Karl
--
Working Without a .NET?
http://classicvb.org/petition


Lee Peedin

2005-04-27, 3:55 pm

Karl,
I'm going to stick my nose in here again (where it probably doesn't
belong) and ask a stupid question. Maybe there's a way to do it in
pure VB code, but I know one can use WMI to monitor the print queue -
with that in mind, if the OP thinks he is overloading the queue,
simply monitor it and not send anything else until it is empty. ??

Lee

On Wed, 27 Apr 2005 09:33:29 -0700, "Karl E. Peterson" <karl@mvps.org>
wrote:

>Mary wrote:
>
>Dunno what that Pdf1 object you have there may be, but it sounds like you've found a
>limitation with it. Maybe it has some sort of "wait" functionality? Another choice
>would be to pass each document to a function like this:
>
> 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
>
> Public Function PrintDoc(ByVal DocFile As String) As Long
> ' Uses the "print" verb as defined in the registry
> PrintDoc = ShellExecute(0&, "print", DocFile, vbNullString, vbNullString,
>vbNormalFocus)
> End Function
>
>Later... Karl


Karl E. Peterson

2005-04-27, 8:55 pm

Hi Lee --

Monitoring the print queue from VB is a bit involved, but far from impossible...

http://vb.mvps.org/samples/PrnInfo

That sample is timer based, to avoid the issues with infinite waits and
single-threading. I haven't looked at using WMI for same, though would be interested
in a reference?

This certainly didn't sound like a print queue overflow, though. More likely, the
object in question is getting with repeated requests. If the queue was at
fault, it likely would be printing the first two, rather than the last two, right?
Your idea of pacing the output is along the same lines I was suggesting, though, and
would probably serve the same result.

Thanks... Karl
--
Working Without a .NET?
http://classicvb.org/petition


Lee Peedin wrote:[color=darkred]
> Karl,
> I'm going to stick my nose in here again (where it probably doesn't
> belong) and ask a stupid question. Maybe there's a way to do it in
> pure VB code, but I know one can use WMI to monitor the print queue -
> with that in mind, if the OP thinks he is overloading the queue,
> simply monitor it and not send anything else until it is empty. ??
>
> Lee
>
> On Wed, 27 Apr 2005 09:33:29 -0700, "Karl E. Peterson" <karl@mvps.org>
> wrote:
>



Lee Peedin

2005-04-27, 8:55 pm

Karl,
Here is a bit of scripting code I used as a model:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colPrintJobs = objWMIService.ExecQuery _
("SELECT * FROM Win32_PrintJob")
For Each objPrintJob in colPrintJobs
intTotalJobs = intTotalJobs + 1
intTotalPages = intTotalPages + objPrintJob.TotalPages
If objPrintJob.TotalPages > intMaxPrintJob Then
intMaxPrintJob = objPrintJob.TotalPages
End If
Next
Wscript.Echo "Total print jobs in queue: " & intTotalJobs
Wscript.Echo "Total pages in queue: " & intTotalPages
Wscript.Echo "Largest print job in queue: " & intMaxPrintJob


In my particuliar application I am printing to PDF. Once the PDF is
complete I immediately email the document. What I was finding was
that the application most likely finishes way before the PDF document
has left the queue. So I simply go in a loop and wait for the queue
to empty. Of course it's a little more involved than that, but you
get the idea. :-)

I'd have to agree that the OP's problem is most likely not an overload
of the queue, but confusion in the queue.

BTW: I've also found that by setting app.title, my document in the
queue and finished PDF uses its value.

Lee

On Wed, 27 Apr 2005 11:26:55 -0700, "Karl E. Peterson" <karl@mvps.org>
wrote:

>Hi Lee --
>
>Monitoring the print queue from VB is a bit involved, but far from impossible...
>
> http://vb.mvps.org/samples/PrnInfo
>
>That sample is timer based, to avoid the issues with infinite waits and
>single-threading. I haven't looked at using WMI for same, though would be interested
>in a reference?
>
>This certainly didn't sound like a print queue overflow, though. More likely, the
>object in question is getting with repeated requests. If the queue was at
>fault, it likely would be printing the first two, rather than the last two, right?
>Your idea of pacing the output is along the same lines I was suggesting, though, and
>would probably serve the same result.
>
>Thanks... Karl


Karl E. Peterson

2005-04-27, 8:55 pm

Hi Lee --

That's almost too easy. <g> How do you control which queue is queried? This sample
might prove useful for another task that a friend of mine asked about the other day -
printing multiple PDFs in a particular order. I really think the only thing that he
needs to do is wait until one gets submitted to the queue, before moving on to the
next. Is this (usage/properties) actually documented anywhere?

Thanks... Karl


Lee Peedin wrote:[color=darkred]
> Karl,
> Here is a bit of scripting code I used as a model:
>
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer &
> "\root\cimv2")
> Set colPrintJobs = objWMIService.ExecQuery _
> ("SELECT * FROM Win32_PrintJob")
> For Each objPrintJob in colPrintJobs
> intTotalJobs = intTotalJobs + 1
> intTotalPages = intTotalPages + objPrintJob.TotalPages
> If objPrintJob.TotalPages > intMaxPrintJob Then
> intMaxPrintJob = objPrintJob.TotalPages
> End If
> Next
> Wscript.Echo "Total print jobs in queue: " & intTotalJobs
> Wscript.Echo "Total pages in queue: " & intTotalPages
> Wscript.Echo "Largest print job in queue: " & intMaxPrintJob
>
>
> In my particuliar application I am printing to PDF. Once the PDF is
> complete I immediately email the document. What I was finding was
> that the application most likely finishes way before the PDF document
> has left the queue. So I simply go in a loop and wait for the queue
> to empty. Of course it's a little more involved than that, but you
> get the idea. :-)
>
> I'd have to agree that the OP's problem is most likely not an overload
> of the queue, but confusion in the queue.
>
> BTW: I've also found that by setting app.title, my document in the
> queue and finished PDF uses its value.
>
> Lee
>
> On Wed, 27 Apr 2005 11:26:55 -0700, "Karl E. Peterson" <karl@mvps.org>
> wrote:
>

--
Working Without a .NET?
http://classicvb.org/petition


Lee Peedin

2005-04-27, 8:55 pm

Karl,
Yes this is all documented in a 1287 page book published by MS.
(Microsoft Windows 2000 Scripting Guide) :-)

So far, everything I have tried has worked on XP as well. The book
also comes with a CD which I have imaged on my laptop (much easier
than carrying around a 1287 page book). There are several examples of
monitoring an individual queue, if you'd like me to look futher, I'd
be glad to.

Lee

On Wed, 27 Apr 2005 12:10:05 -0700, "Karl E. Peterson" <karl@mvps.org>
wrote:
[color=darkred]
>Hi Lee --
>
>That's almost too easy. <g> How do you control which queue is queried? This sample
>might prove useful for another task that a friend of mine asked about the other day -
>printing multiple PDFs in a particular order. I really think the only thing that he
>needs to do is wait until one gets submitted to the queue, before moving on to the
>next. Is this (usage/properties) actually documented anywhere?
>
>Thanks... Karl
>
>
>Lee Peedin wrote:

Karl E. Peterson

2005-04-27, 8:55 pm

Lee Peedin wrote:
> Karl,
> Yes this is all documented in a 1287 page book published by MS.
> (Microsoft Windows 2000 Scripting Guide) :-)


Wow, I think I found bunches of it online...

Microsoft Windows 2000 Scripting Guide - Monitoring Printers, Print Queues, and Print
Jobs
http://www.microsoft.com/technet/sc...s_prn_wtfx.mspx

I'll have to see if I can dig up a copy of that book. Not sure how it got past me
before. I think, when it was "fresh", I was still concerned about also support
9x/NT4, and WMI wasn't as practical.

Thanks... Karl


[color=darkred]
> So far, everything I have tried has worked on XP as well. The book
> also comes with a CD which I have imaged on my laptop (much easier
> than carrying around a 1287 page book). There are several examples of
> monitoring an individual queue, if you'd like me to look futher, I'd
> be glad to.
>
> Lee
>
> On Wed, 27 Apr 2005 12:10:05 -0700, "Karl E. Peterson" <karl@mvps.org>
> wrote:
>

--
Working Without a .NET?
http://classicvb.org/petition


Lee Peedin

2005-04-27, 8:55 pm

You're quite welcome Karl, and just remember this came from a former
Object Rexx programmer - now an ooRexx programmer. :-)

Lee

On Wed, 27 Apr 2005 12:58:03 -0700, "Karl E. Peterson" <karl@mvps.org>
wrote:
[color=darkred]
>Lee Peedin wrote:
>
>Wow, I think I found bunches of it online...
>
>Microsoft Windows 2000 Scripting Guide - Monitoring Printers, Print Queues, and Print
>Jobs
>http://www.microsoft.com/technet/sc...s_prn_wtfx.mspx
>
>I'll have to see if I can dig up a copy of that book. Not sure how it got past me
>before. I think, when it was "fresh", I was still concerned about also support
>9x/NT4, and WMI wasn't as practical.
>
>Thanks... Karl
>
>
>

J French

2005-04-28, 3:56 am

On Wed, 27 Apr 2005 07:38:12 -0700, "=?Utf-8?B?TWFyeQ==?="
<Mary@discussions.microsoft.com> wrote:

>Hi!
>
>I'm trying to do a Visual Basic program that allows me print all .pdf
>documents inside a folder. If there is only one .pdf document inside the
>folder my program run propertly, but unfortunately when there are several
>.pdf documents it only print the last 2 ones. I supose that it happens
>because I send too much information to the printer, but Is there any way of
>sending data to the printer and sleeping the program until the printer
>finished printing?


I am suspicious about your PDF Object
- if that is (badly) written in VB (or anything else) it could be
mucking up your Dir() loop

Try getting all the file names into an array
- and then printing them
Lee Peedin

2005-04-28, 3:56 am

According to this link:
http://techrepublic.com.com/5208-62...threadID=170514

"NOTE: You do not need the full version of Acrobat to use the pdf.ocx
control. It comes with Reader as well (though on your development
system you will want to reference the newest full Acrobat one you
have). "

Wonder where pdf.ocx comes from?
Lee

On Thu, 28 Apr 2005 00:58:15 +0000 (UTC), erewhon@nowhere.uk (J
French) wrote:

>On Wed, 27 Apr 2005 07:38:12 -0700, "=?Utf-8?B?TWFyeQ==?="
><Mary@discussions.microsoft.com> wrote:
>
>
>I am suspicious about your PDF Object
>- if that is (badly) written in VB (or anything else) it could be
>mucking up your Dir() loop
>
>Try getting all the file names into an array
>- and then printing them


J French

2005-04-28, 3:56 am

On Thu, 28 Apr 2005 01:33:42 GMT, Lee Peedin
<lpeedinREMOVE@UPPERCASEnc.rr.com> wrote:

>According to this link:
>http://techrepublic.com.com/5208-62...threadID=170514
>
>"NOTE: You do not need the full version of Acrobat to use the pdf.ocx
>control. It comes with Reader as well (though on your development
>system you will want to reference the newest full Acrobat one you
>have). "


>Wonder where pdf.ocx comes from?


Hmm, Adobe - comes with Acrobat Reader

Even so, that Dir() loop is asking for trouble

Probably worth looking at any documentation
Lee Peedin

2005-04-28, 3:56 am

I guess what "threw me" in the link I quoted was that the inquirer had
the full Acrobat - why would he need an external .ocx to print files.
My full version of Acrobat allows me to print "anything" to PDF.

Lee

On Thu, 28 Apr 2005 02:17:04 +0000 (UTC), erewhon@nowhere.uk (J
French) wrote:

>On Thu, 28 Apr 2005 01:33:42 GMT, Lee Peedin
><lpeedinREMOVE@UPPERCASEnc.rr.com> wrote:
>
>
>
>Hmm, Adobe - comes with Acrobat Reader
>
>Even so, that Dir() loop is asking for trouble
>
>Probably worth looking at any documentation


J French

2005-04-28, 8:55 am

On Thu, 28 Apr 2005 03:01:44 GMT, Lee Peedin
<lpeedinREMOVE@UPPERCASEnc.rr.com> wrote:

>I guess what "threw me" in the link I quoted was that the inquirer had
>the full Acrobat - why would he need an external .ocx to print files.
>My full version of Acrobat allows me to print "anything" to PDF.


Actually I misunderstood your post entirely <g>
- I thought that you were pointing out that the PDF.OCX is from Adobe

I scouted it out on my drive and had a look inside the OCX with an
editor to check its origins (just curiousity)

It is a redistributable tool for viewing PDFs and chucking them at the
printer

Realistically there is no way something like that should skip files,
so this business of waiting until a job is completed is rather
spurious.

It is possible (but unlikely) that the OP has a dodgy printer driver

It is also possible that loading another document zaps the current
printing - but that would be a little arcane.

I've managed to stick it in a project, it does not appear to have any
'printing flag' or events.

Frankly I think that the OP should add a manual delay, to see whether
the 'lost' files really are lost
Karl E. Peterson

2005-04-28, 3:57 pm

J French wrote:
> Even so, that Dir() loop is asking for trouble


That sounds like a valid warning, as well.
--
Working Without a .NET?
http://classicvb.org/petition


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com