trevix
Programmer
- Dec 4, 2012
- 1
I'm a newby at VBscript, but I often have to use it from LiveCode (RunRev) to do things...
After 2 (!) days of search for what I believe should be a very popular script, that is, run a VBscript that given a file path will print (to the default printer) the Pdf and get some kind of feedback, I came up with this two script.
Script 1: print the file. Replace the <thedocpath> with the path to the pdf file.
This script works fine except that I dont know how to print more then 1 copy of the file so I have to loop trough it, and this slows things. As I said I run it from Livecode, but it works also if you save it to a text.vbs file and double clik it.
Script 2) Check the spooler. Replace <docname> with the name of your pdf file.
This give me some kind of feedbak (return =1 print ok, =0 print NO ok)
Since there may be more then one copies of the same print (because I had to loop), I cannot return 1 right after the printing start, but I have to wait until all the copies got spooled...
I believe that my code is kind of messy so I would be gratefull if someone can streamline and clean it....!
But my question is: How can I print more then one copy of the same file, without looping ?
Thanks for any help or suggestion.
Trevix
After 2 (!) days of search for what I believe should be a very popular script, that is, run a VBscript that given a file path will print (to the default printer) the Pdf and get some kind of feedback, I came up with this two script.
Script 1: print the file. Replace the <thedocpath> with the path to the pdf file.
Code:
on error resume next
set oWsh = CreateObject ("Wscript.Shell")
for i = 1 to <numCopies>
oWsh.run """AcroRd32.exe"" /p /h" &"<thedocpath>",,false
if Err.Number <> 0 then
result = "Error: " & Err.Number
end if
next
set oWsh = nothing
This script works fine except that I dont know how to print more then 1 copy of the file so I have to loop trough it, and this slows things. As I said I run it from Livecode, but it works also if you save it to a text.vbs file and double clik it.
Script 2) Check the spooler. Replace <docname> with the name of your pdf file.
This give me some kind of feedbak (return =1 print ok, =0 print NO ok)
Since there may be more then one copies of the same print (because I had to loop), I cannot return 1 right after the printing start, but I have to wait until all the copies got spooled...
Code:
Option Explicit
On Error Resume Next
Dim strComputer, wmiNS, wmiQuery, objWMIService, colItems, objItem
Dim Counter, Risultato, result
Dim theDocName, theNumID, theNumprovID
Counter = 0
Risultato = 0
theNumID = 0
strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * from win32_PrintJob"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
' start a query loop
do
Set colItems = objWMIService.ExecQuery(wmiQuery)
Counter = Counter +1 'this increases up to 8000 (8 sec to avoid lock...)
For Each objitem In colItems
if objitem.Document = "<docname>" then 'compare the doc name on the spooler with the name of my doc
if objitem.JobID > theNumID then 'JobID is a progressive number assigned by the spooler (start with 0 so should not be a problem)
Risultato = Risultato + 1 'get count of prints (should end equal to <NumOfCopies>)
theNumID = objitem.JobID
theNumprovID = theNumprovID & " " & theNumID
end if
end if
Next
loop until Risultato >= <NumOfCopies> or Counter >= 8000
' we got the last copy or a timeout
if Counter >= 8000 and Risultato = 0 then
Risultato = 0
else
Risultato = 1
end if
set colItems = nothing
set objWMIService = nothing
result = Risultato
I believe that my code is kind of messy so I would be gratefull if someone can streamline and clean it....!
But my question is: How can I print more then one copy of the same file, without looping ?
Thanks for any help or suggestion.
Trevix