| 发表于:2007-06-29 13:03:206楼 得分:0 |
public function checkprinter(printername as string, printerstr as string, jobstr as string) as string 'dim printername as string dim hprinter as long dim bytebuf as long dim bytesneeded as long dim pi2 as printer_info_2 dim ji2 as job_info_2 dim printerinfo() as byte dim jobinfo() as byte dim result as long dim lasterror as long dim tempstr as string dim numji2 as long dim pdefaults as printer_defaults dim i as integer 'set a default return value if no errors occur. checkprinter = "printer info retrieved " ' '================================== '此处做一简单的修改,让本函数遍历所有的打印机 ' printername = printer.devicename '============================= 'set desired access security setting. pdefaults.desiredaccess = printer_access_use 'call api to get a handle to the printer. result = openprinter(printername, hprinter, pdefaults) if result = 0 then 'if an error occurred, display an error and exit sub. checkprinter = "cannot open printer " & printername & _ ", error: " & err.lastdllerror exit function end if 'init bytesneeded bytesneeded = 0 'clear the error object of any errors. err.clear 'determine the buffer size that is needed to get printer info. result = getprinter(hprinter, 2, 0&, 0&, bytesneeded) 'check for error calling getprinter. if err.lastdllerror <> error_insufficient_buffer then 'display an error message, close printer, and exit sub. checkprinter = " > getprinter failed on initial call! < " closeprinter hprinter exit function end if redim printerinfo(1 to bytesneeded) bytebuf = bytesneeded 'call getprinter to get the status. result = getprinter(hprinter, 2, printerinfo(1), bytebuf, _ bytesneeded) 'check for errors. if result = 0 then 'determine the error that occurred. lasterror = err.lastdllerror() checkprinter = "couldn 't get printer status! error = " _ & lasterror closeprinter hprinter exit function end if copymemory pi2, printerinfo(1), len(pi2) printerstr = checkprinterstatus(pi2.status) printerstr = printerstr & "printer name = " & _ getstring(pi2.pprintername) & vbcrlf printerstr = printerstr & "printer driver name = " & _ getstring(pi2.pdrivername) & vbcrlf printerstr = printerstr & "printer port name = " & _ getstring(pi2.pportname) & vbcrlf debug.print printerstr result = enumjobs(hprinter, 0&, &hffffffff, 2, byval 0&, 0&, _ bytesneeded, numji2) if bytesneeded = 0 then jobstr = "no print jobs! " else redim jobinfo(0 to bytesneeded) result = enumjobs(hprinter, 0&, &hffffffff, 2, jobinfo(0), _ bytesneeded, bytebuf, numji2) if result = 0 then 'get and display error, close printer, and exit sub. lasterror = err.lastdllerror checkprinter = " > enumjobs failed on second call! < error = " _ & lasterror closeprinter hprinter exit function end if for i = 0 to numji2 - 1 ' loop through jobs and walk the buffer copymemory ji2, jobinfo(i * len(ji2)), len(ji2) ' list info available on jobs. debug.print "job id " & vbtab & ji2.jobid debug.print "name of printer " & vbtab & _ getstring(ji2.pprintername) debug.print "name of machine that created job " & vbtab & _ getstring(ji2.pmachinename) debug.print "print job owner 's name " & vbtab & _ getstring(ji2.pusername) debug.print "name of document " & vbtab & getstring(ji2.pdocument) debug.print "name of user to notify " & vbtab & _ getstring(ji2.pnotifyname) debug.print "type of data " & vbtab & getstring(ji2.pdatatype) debug.print "print processor " & vbtab & _ getstring(ji2.pprintprocessor) debug.print "print processor parameters " & vbtab & _ getstring(ji2.pparameters) debug.print "print driver name " & vbtab & _ getstring(ji2.pdrivername) debug.print "print job 'p ' status " & vbtab & _ getstring(ji2.pstatus) debug.print "print job status " & vbtab & ji2.status debug.print "print job priority " & vbtab & ji2.priority debug.print "position in queue " & vbtab & ji2.position debug.print "earliest time job can be printed " & vbtab & _ ji2.starttime debug.print "latest time job will be printed " & vbtab & _ ji2.untiltime debug.print "total pages for entire job " & vbtab & ji2.totalpages debug.print "size of job in bytes " & vbtab & ji2.size debug.print "elapsed print time " & vbtab & ji2.time debug.print "pages printed so far " & vbtab & ji2.pagesprinted 'display basic job status info. jobstr = jobstr & "job id = " & ji2.jobid & _ vbcrlf & "total pages = " & ji2.totalpages & vbcrlf tempstr = " " 'clear if ji2.pstatus = 0& then ' if pstatus is null, check status. if ji2.status = 0 then tempstr = tempstr & "ready! " & vbcrlf else 'check for the various print job states. if (ji2.status and job_status_spooling) then tempstr = tempstr & "spooling " end if if (ji2.status and job_status_offline) then tempstr = tempstr & "off line " end if if (ji2.status and job_status_paused) then tempstr = tempstr & "paused " end if if (ji2.status and job_status_error) then tempstr = tempstr & "error " end if if (ji2.status and job_status_paperout) then tempstr = tempstr & "paper out " end if if (ji2.status and job_status_printing) then tempstr = tempstr & "printing " end if if (ji2.status and job_status_user_intervention) then tempstr = tempstr & "user intervention needed " end if if len(tempstr) = 0 then tempstr = "unknown status of " & ji2.status end if end if else tempstr = ptrctovbstring(ji2.pstatus) end if jobstr = jobstr & tempstr & vbcrlf debug.print jobstr & tempstr next i end if closeprinter hprinter end function 本代码取自网上,略有改动 | | |
|