Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change default printer

Printers

How to change default printer

by  Mike Gagnon  Posted    (Edited  )
There are a few ways to do this, one is a two lines of code (assuming that Windows Scripting v5.6 is installed):
oNet = CREATEOBJECT("WScript.Network")
oNet.SetDefaultPrinter("\\slp_nt_termserv\Panasonic Printer")

This also works on local printers.
oNet.SetDefaultPrinter("Epson FX-80+")
_____________________________________________________
Or using an API call like this:

Code:
 Printers = ""
   Number = 0
   Defa_print = ""
   DO GETPRINT WITH Printers, Number, Defa_print
   *Printers   - is an array with all the printers installed.
   *Number     - is the number of printers in the WIN.INI file.
   *Defa_print - is the default printer in the WIN.INI file.

   DO PUTPRINT WITH Printers, Number, NEW_DEFA
   *Printers   - is the return array from the GETPRINT.
   *Number     - is the return number of printers from GETPRINT.
   *NEW_DEFA   - is the new default string that will be put directly into
   *             the WIN.INI file (e.g., "HP LaserJet III,HPPCL5MS,LPT1;")
   *             This should only be done if you know exactly what should
   *             be in the Default printer string.


   PROCEDURE getprint
   PARAMETER DEVICE, NUMBER, dfltprnt
   #DEFINE buflen 2048
   PRIVATE m.in_talk, m.dcount, m.retbuf, m.bytes, m.thisdevice

   IF PARAMETERS() < 3
      WAIT WINDOW "This procedure requires 3 parameters"
      RETURN
   ENDIF

   IF FILE(SYS(2004)+"FOXTOOLS.FLL")
      SET LIBRARY TO (SYS(2004)+"foxtools.fll") ADDITIVE
   ELSE
      WAIT WINDOW "GETPRINT requires the FoxTools library."
      RETURN
   ENDIF

   IF SET("TALK") = "ON"
      SET TALK OFF
      m.in_talk = "ON"
   ELSE
      m.in_talk = "OFF"
   ENDIF
   m.retbuf = REPLICATE(CHR(0),buflen)
   m.bytes = getprostrg("devices",0,CHR(0),@m.retbuf,buflen)
   m.dcount = 0
   m.retbuf = LEFT(m.retbuf,m.bytes)
   DO WHILE CHR(0) $ m.retbuf
      m.thisdevice = LEFT(m.retbuf,AT(CHR(0),m.retbuf)-1)
      IF LEFT(m.thisdevice,1) <> CHR(0)
         m.dcount = m.dcount + 1
         DIMENSION device[m.dcount,3]
         device[m.dcount,1] = m.thisdevice
      ENDIF
      m.retbuf = SUBSTR(m.retbuf,AT(CHR(0),m.retbuf)+1)
   ENDDO
   FOR m.j = 1 TO m.dcount
      retbuf = REPLICATE(CHR(0),256)
      m.bytes = ;
         getprostrg("devices",device[m.j,1],CHR(0),@m.retbuf,256)
      m.retbuf = LEFT(m.retbuf,m.bytes)
      device[m.j,2] = m.retbuf

      retbuf = REPLICATE(CHR(0),256)
      m.bytes = ;
         getprostrg("PrinterPorts",device[m.j,1],CHR(0),@m.retbuf,256)
      m.retbuf = LEFT(m.retbuf,m.bytes)
      device[m.j,3] = m.retbuf
   ENDFOR
   m.number = m.dcount
   retbuf = REPLICATE(CHR(0),256)
   m.bytes = getprostrg("windows","device",CHR(0),@m.retbuf,256)
   m.retbuf = LEFT(m.retbuf,m.bytes)
   m.dfltprnt = m.retbuf

   SET TALK &in_talk
   PROCEDURE listprint
   PARAMETER DEVICE
   #DEFINE buflen 2048
   PRIVATE m.in_talk,m.i
   IF FILE(SYS(2004)+"FOXTOOLS.FLL")
      SET LIBRARY TO (SYS(2004)+"foxtools.fll") ADDITIVE
   ELSE
      WAIT WINDOW "LISTPRINT requires the FoxTools library."
      RETURN
   ENDIF
   IF SET("TALK") = "ON"
      SET TALK OFF
      m.in_talk = "ON"
   ELSE
      m.in_talk = "OFF"
   ENDIF

   DIMENSION DEVICE(1)
   NUMBER = 0
   dflt = ""

   DO getprint WITH DEVICE, NUMBER, dflt
   CLEAR
   ? ALLTRIM(STR(m.number,3))+" installed printer devices in win.ini:"
   FOR m.i = 1 TO NUMBER
      ? "  "+device[m.i,1]+"="+device[m.i,3]
   ENDFOR
   ?
   ? "Default printer is "+dflt
   SET TALK &in_talk
   PROCEDURE putprint
   PARAMETER DEVICE, NUMBER, dflt, portnum
   IF FILE(SYS(2004)+"FOXTOOLS.FLL")
      SET LIBRARY TO (SYS(2004)+"foxtools.fll") ADDITIVE
   ELSE
      WAIT WINDOW "PUTPRINT requires the FoxTools library."
      RETURN
   ENDIF

   IF TYPE("dflt") = "N"
      IF PARAMETERS() < 4
         portnum = 1
      ENDIF
      m.strg = device[m.dflt,1]+","+getport(device[m.dflt,2],portnum)
   ELSE
      m.strg = m.dflt
   ENDIF
   =putprostrg("windows","device",m.strg)
   =putprostrg("devices",0,CHR(0))
   =putprostrg("PrinterPorts",0,CHR(0))
   FOR m.i = 1 TO NUMBER
      =putprostrg("devices",device[m.i,1],device[m.i,2])
      =putprostrg("PrinterPorts",device[m.i,1],device[m.i,3])
   ENDFOR
   FUNCTION putprostrg
   PARAMETER SECTION, entry, string
   fn = regfn("WRITEPROFILESTRING","CCC","I")
   RETURN callfn(fn,SECTION,entry,string)
   FUNCTION getprostrg
   PARAMETER SECTION, entry, dflt, buffer, blen
   fn = regfn("GETPROFILESTRING","CCC@CI","I")
   RETURN callfn(fn,SECTION,entry,dflt,@buffer,blen)
   FUNCTION getport
   PARAMETER m.pstrg, m.pnum
   m.retstrg = SUBSTR(m.pstrg,1,AT(',',m.pstrg))
   IF OCCURS(',',m.pstrg) >= m.pnum
      m.portstrg = SUBSTR(m.pstrg,AT(',',m.pstrg,m.pnum)+1)
   ELSE
      m.portstrg = SUBSTR(m.pstrg,AT(',',m.pstrg,1)+1)
   ENDIF

   IF AT(',',m.portstrg) > 0
      m.portstrg = LEFT(m.portstrg,AT(',',m.portstrg)-1)
   ENDIF

   RETURN m.retstrg + m.portstrg


Mike Gagnon
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top