Some helpful code for printing from the command line

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Some helpful code for printing from the command line

Post by kpassaur » Wed Nov 21, 2007 3:38 pm

Been a while since I contributed anything so here are some tips on printing. The notes below will go over how to get a list of available printers, finding the default printer, resetting the default printer, printing image files (you could use any file type). Anyway, a dialog to select the printer to use, and the folder to process and a loop is all that is needed to print all the files in a directory or subdirectory on any printer desired. This is ideal for attorneys that print thousands of tiff images.

If you are wondering why it is done this way as you could just specify a printer to use on the command line, it is becasue not all applications allow that. They only print to the default printer. So now you can change it and change it back again.


Anyway, everthing should now be in one place for reference.


//_________________________ Get a list of available Printers and write there names to a text file
IffileExists>C:\BatchPrint\printerdetails.txt
DeleteFile>C:\BatchPrint\printerdetails.txt
Endif

Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run Program>cmd /c cscript %SYS_DIR%\prnmngr.vbs -l >C:\BatchPrint\printerdetails.txt
Let>k=1
Let>PrinterList=
Label>start
ReadLn>C:\BatchPrint\printerdetails.txt,k,rpline
If>rpline=##EOF##,finish
Position>Printer name,%rpline%,1,pname
If>%pname%=1
StringReplace>%rpline%,Printer name ,,Printername
Let>PrinterList=%PrinterList%%Printername%
ConCat>PrinterList,%CRLF%
Endif
Let>k=k+1
Goto>start
Label>finish
//________________________


///_______________________Finds default printer
RegistryReadKey>HKEY_CURRENT_USER,Software\Microsoft\Windows NT\CurrentVersion\Windows,Device,DefPrinter
Let>DefPrinter={Copy(%DefPrinter%,1,Pos(",",%DefPrinter%)-1)}
Let>PRINTERTOUSE=DefPrinter
//________________________

// _______________________Add a dialog to select a printer here (read the list created above for printer names) and a folder to get the images (or other file type) from

//_______________________Reset the default Printer - in this case dp is the printer (selected in the dialog that you created)

Let>dp=%Dialog1.msComboBox1%
Run Program>rundll32 printui.dll,PrintUIEntry /y /n "%dp%"
//_______________________


//_______________________ Print with this for image files - bmp - jpg - tif - Check out Windows Picture and Fax viewer for all types supported - anything that can be printed from a command line will work here such as Word Docs, txt files (Notepad).


Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run Program>rundll32.exe C:\WINDOWS\System32\shimgvw.dll,ImageView_PrintTo /pt "%fileandpath%" "%dp%"
//_______________________

///______________________ Sets the printer Back to default

Run Program>rundll32 printui.dll,PrintUIEntry /y /n "%DefPrinter%"
//________________________

Hope someone finds this useful.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Nov 21, 2007 5:30 pm

kpassaur,

Thank you very much. I have been intending to look into this very subject. From what I've read this will be useful. I'll know more when I a chance to experiment with it.

Later,
Dick

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Feb 17, 2011 6:10 pm

Hi all,
Keith's script above has been very useful for me, I have used it in multiple scripts. The problem now is all new computers I buy are Windows 7 operating system and the script fails under Windows 7. I have Windows 7 computers in the facility but I don't have one myself nor do I have easy access to one.

Has anyone or could anyone make this script work under Windows 7?

One thing I am aware of is that the VBScript used by Keith's script was provided in Win Xp but does not seem to exist on Windows 7. In Win XP there was a small group of useful scripts that were located in the \Windows\System32 folder. The names of the files are

eventquery.vbs
pagefileconfig.vbs
prncnfg.vbs
prndrvr.vbs
prnjobs.vbs
prnmngr.vbs
prnport.vbs
prnqctl.vbs
pubprn.vbs
SetACL.vbs

Keith's script uses prnmngr.vbs to generate a text file from which a list of available printers was created. What I know is that simply copying these files to the Windows 7 system32 folder does not make the script function.

Thanks,
Dick

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Printer List

Post by kpassaur » Thu Feb 17, 2011 7:31 pm

Dick,

It will work on Vista and Seven. Below is a couple of portions of a script that I use. Hopefully this will help. I used this to get the Printer list and set the index value in a dialog.

The value %APB% is read from an ini file and is the name of a printer
At the end the list of printers is %Printerlist%
The index value of the printer is %i%

So with this you can get a name of a printer, set the list in a dialog and set the dialog to printer read in the ini.

You need to make sure the VB runtime files are installed, create a folder for the files (I called it ProgramFolder)

Let>ProgramFolder=C:\test

Place vbrun60sp6.exe in the folder along with prnmngr.vbs


VBSTART
Const ssfCOMMONAPPDATA = &H23

Function GetSpecialFolder(FolderID)
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(FolderID)
Set objFolderItem = objFolder.Self
GetSpecialFolder = objFolderItem.Path
End Function
VBEND

VBEval>GetSpecialFolder(ssfCOMMONAPPDATA),pathComAppData
Let>settinsdir=%pathComAppData%\test
IfDirExists>%settinsdir%,haveappdir
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
RunProgram>cmd /c MD "%settinsdir%"
Label>haveappdir

IfFileExists>%SYS_DIR%\msvbvm50.dll,havevbruntime
IfFileExists>%SYS_DIR%\msvbvm60.dll,havevbruntime

Ask>The VBruntime files from Microsoft Cannot be found and they are required - Intall them now?,irt
If>%irt%=YES
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
RunProgram>%ProgramFolder%\vbrun60sp6.exe
Else
MDL>The program cannot run without the VBruntime files and it will now exit
Exit>0
Endif
Label>havevbruntime


SRT>GetPrinterList
let>e=0
Deletefile>%settinsdir%\printerdetails.txt
ChangeDirectory>%SYS_DIR%
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
RunProgram>cmd /c cscript "%ProgramFolder%\prnmngr.vbs" -l >"%settinsdir%\printerdetails.txt"



Let>k=1
Let>PrinterList=%CRLF%
Label>startpl
IfFileExists>%settinsdir%\printerdetails.txt,haveprinteroutput
Let>e=e+1
If>e=10
Let>PrinterList=Cannot Find Printers
Goto>endpl
Endif
Wait>1
Goto>startpl

Label>haveprinteroutput
ReadLn>%settinsdir%\printerdetails.txt,k,rpline
If>rpline=##EOF##,finishfl
Position>Printer name,%rpline%,1,pname
If>%pname%=1
StringReplace>%rpline%,Printer name ,,Printername
Let>PrinterList=%PrinterList%%Printername%
ConCat>PrinterList,%CRLF%
Endif
Let>k=k+1
Goto>startpl
Label>finishfl
If>%DP%=,endpl

Separate>%PrinterList%,%CRLF%,printarray
Let>tp=%printarray_count%
Let>i=0
If>file_names_count=0,endpl
Let>k=0
Repeat>k
Let>k=k+1
Let>Printername=printarray_%k%
If>%Printername%=%APB%
Let>i=k-1
Endif
If>%Printername%=%DP%
Let>j=k-1
Endif


Until>k,printarray_count
Label>endpl

End>GetPrinterList

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Feb 17, 2011 8:22 pm

Thanks Keith,

I'll take a look and let you know.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts