Jump to content

Set Default Printer


Recommended Posts

My project was cruising along well until I just discovered a bug in the database software that automatically reverts to the default windows printer no matter what you tell it to do. The third party fix is $1000 for a redistribution lisence. Basically I am printing from the database to a virtual printer and Ghostscript which saves with a static directory/name. The database then initiates an AutoIT.exe that renames the PDF, moves & resizes the window. The PDF is used as a print preview with the option to save as if the file should be kept, print or just close. A script runs upon closing the database that deletes all *.pdf documents in the folder printed to. The distiller cannot be the default printer. A suitable workaround would be to script changing the default printer for the duration of the process. Can AutoIT do this?

Edited by onefish
Link to comment
Share on other sites

Can AutoIT do this?

<{POST_SNAPBACK}>

RunWait(@SystemDir & '\RunDLL32.EXE printui.dll,PrintUIEntry /y /n "[Printer Display Name]"')

or if you really want to show off:

DllCall("printui.dll", "none", "PrintUIEntryW", _ 
    "hwnd", 0, _ 
    "ptr", 0, _ 
    "wstr", '/y /n "' & $sPrinterName & '"', _ 
    "int", @SW_SHOWNORMAL)
Edited by SumTingWong
Link to comment
Share on other sites

Wow, I have got alot to learn. As it turns out (I think from observing the behavior) the database application loads it's settings to the default printer when it is launched. I believe I need to write a script that will change the default printer to the distiller then launch the database application, wait for a few seconds for it to load, then change the default printer back to the original one. Launching the database.exe is the only part of this that I have a grasp of at the moment. I would like to understand these scripts a bit better so I can:

-declare a variable for the original default printer. I assume it would have to come from a DllCall but I do not understand the nature of the params and types. Do you know of a source for this information?

-declare a variable for the distiller (I can work this one out)

-use one of the scripts above to perform the first change

-then revert.

I am very keen to learn these things and I really appreciate your assistance. Thank you.

Link to comment
Share on other sites

-declare a variable for the original default printer.  I assume it would have to come from a DllCall but I do not understand the nature of the params and types.  Do you know of a source for this information?

<{POST_SNAPBACK}>

You can read the Default Printer from the registry.

sRegKey = "HKCU\Software\Microsoft\Windows Nt\CurrentVersion\Windows\Device"

HTH, Reinhard

Edited by ReFran
Link to comment
Share on other sites

Reinhard,

So is this bit of script correct? Will this actually change the default printer through the "RunWait" command?

$default = RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows Nt\CurrentVersion\Windows\Device" , "DefaultPrinter")

$distiller = "PDFCreator"

RunWait(@SystemDir & "\RunDLL32.EXE printui.dll,PrintUIEntry /y /n " & $distiller )

;Script that will launch the database application and pause for 5 seconds or so.

RunWait(@SystemDir & "\RunDLL32.EXE printui.dll,PrintUIEntry /y /n " & $default )

I am unsure about the value I should be reading from the registry (DefaultPrinter). Your code also used "sRegKey", I am unsure where to use this parameter. Thank you for your assistance.

Link to comment
Share on other sites

Any more help??  This is temporarily driving me mad.  Everything works in a 4 month long project except changing the default printer while the database application is executed.

<{POST_SNAPBACK}>

A little bit testing would help.

The RegKey is HKCU instead of HKLM (sorry). The Code snipet is from VBS. Here how to get it with Autoit - at least under Win2k.

$default = RegRead("HKCU\Software\Microsoft\Windows Nt\CurrentVersion\Windows", "Device")

MsgBox("","Test",$default)

I think to use $default with RunDll32, you have to shorten it to only the printer name.

HTH, Reinhard

Edited by ReFran
Link to comment
Share on other sites

OK, I have mucked around and can change the default printer in the registry easy enough. This is the first time I have mucked around with registry settings and I was a bit hesitant. My script is working on 2k and XP. Any traps I should be aware of post 95 with HKCU printer settings? Test script:

$default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows Nt\CurrentVersion\Windows", "Device")

$new = "PDFCreator," & RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows Nt\CurrentVersion\Devices", "PDFCreator")

RegWrite( "HKEY_CURRENT_USER\Software\Microsoft\Windows Nt\CurrentVersion\Windows", "Device" , "REG_SZ" , $new )

MsgBox(0, "YES/N0", $default & @CR & $new & @CR & @error)

RegWrite( "HKEY_CURRENT_USER\Software\Microsoft\Windows Nt\CurrentVersion\Windows", "Device" , "REG_SZ" , $default )

This changes the default printer and then reverts back after the MsgBox. I will put some error captures in before using in real world.

Edited by onefish
Link to comment
Share on other sites

  • 1 year later...

The earlier post of RunWait(@SystemDir & '\RunDLL32.EXE printui.dll,PrintUIEntry /y /n "[Printer Display Name]"') is great. I had a need for this to change default printer during execution. Was using kixtart to add/delete default printer.

I can use the mentioned to add/change default but how do I check to see if it exist. I have a text file of printers and upon loading want to check to see if printer exist and if not add it. I cannot find appropriate code to determine if printer exist.

Secondly, if I attempt to add a printer and the printer name is invalid Autoit3 will not return an error code. I get a Printers dialog box stating Operation could not be completed. Either the printer name was typed incorrectly, or the specified printer has lost it's connection to the server.

Bottom line: Upon script startup want to check if printer exist, if not add printer but be able to check if successful. Printer name will be supplied via a file and thus check to insure valid name.

Thank you

Link to comment
Share on other sites

  • 2 months later...

Just saw this and just worked with printers.

Took the following code and change it to your need.

HTH, Reinhard

;-----------------------------------------------------------
; Get specific information from all installed printers
; Refran based on a VBS from Christoph Basedau
;-----------------------------------------------------------


$oWMI = ObjGet("winmgmts:")
GetPrinter ("Win32_Printer")

Func GetPrinter($sID)
  $colWBEMSet = $oWMI.InstancesOf($sID)
  msgbox(0,"",$colWBEMSet.count)
  If $colWBEMSet.count = 0 Then
     MsgBox(0,"", "No elements in Win32_Printer found")
     exit
   Else
     For $oElem in $colWBEMSet
         $xFind = ""
         $xtxt = $oElem.GetObjectText_
                     ;msgbox(0,"",$xTxt);= show all information
          $aTxt = Stringsplit($xTxt,";")
          for $x in $atxt; Filter specific information
              if stringInstr(stringleft($x,9),"Name = ") then $xfind = $xfind &$x &"; "
              if stringInstr($x,"DriverName = ") then $xfind = $xFind &$x &"; "
              if stringInstr($x,"PortName = ") then $xfind = $xFind &$x &"; " 
              if stringInstr($xfind,"PortName") then exitLoop;=PortName = last to find
          next
          msgbox(0,"",$xFind)
      Next
   EndIf
EndFunc
Edited by ReFran
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...