; --------------- Choose printer from CSV ------------
; ----------------------------------------------------
? "Choosing printer(s): @time"
; Default printer
$printer="CutePDF"
Dim $printers[10]
$found=0
$hand=freefilehandle()
if $hand>0 and open($hand,"$pdc\netlogon\printers.csv") = 0
  $d=readline($hand)
  while @ERROR = 0 AND $found=0
    $p=split($d,",",10)
    $as=instr($p[0],"*")
    if $as > 0
      if substr($p[0],1,$as-1)=substr($name,1,$as-1)
        $found=1
      Endif
    Else
      if $p[0]=$name
        $found=1
      Endif
    Endif
    If $found=1
      $printer=$p[1]
      $count=ubound($p)
      IF $count > 1
        FOR $k = 2 TO $count
          $printers[$k-2]=$p[$k]
        Next
      endif
    endif
    $d=readline($hand)
  loop
  $d=close($hand)
  If @ERROR
    ? "Error parsing printers.csv (@ERROR)"
  Endif
else
  ? "Could not open $pdc\netlogon\printers.csv (@ERROR)"
endif
If $found=0 
  $def=" by default"
Else
  $def=""
Endif
? "Connecting to printer(s): @time"
$count=ubound($printers)
IF $count > 1
  FOR $k = 0 TO $count
    $pr=$printers[$k]
    IF $pr<>""
      ? "Printer $k is $pr"
      ; If not a UNC path assume is on default printer server
      if substr($pr,1,2)="\\"
        addprinterconnection($pr)
      else 
        if $print<>""
          addprinterconnection("\\$print\$pr")
        else
          addprinterconnection("$pr")
        endif
      endif
    endif
  Next
endif
if substr($printer,1,2)="\\"
  if lcase(substr($printer,3,6))="local\"
    $p=substr($printer,9)
    $dum=setdefaultprinter("$p")
    ? "Printing on local printer $p"
  else
    if addprinterconnection($printer)=0
      $dum=setdefaultprinter("$printer")
      ? "Printing on $printer$def"
      messagebox("Printing on $printer$def",$uname,0,$timeout)
    else
      ? "Could not select printer $printer (@ERROR)"
      messagebox("Could not select printer $printer (@ERROR)",$uname,0,$timeout)
    endif
  endif
else
  if $print<>""
    if addprinterconnection("\\$print\$printer")=0
      $dum=setdefaultprinter("\\$print\$printer")
      messagebox("Printing on $printer$def",$uname,0,$timeout)
    else
      messagebox("Could not select printer $printer (@ERROR)",$uname,0,$timeout)
    endif
  else
    $port=readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\$printer","Port")
    $dum=writevalue("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device","$printer,winspool,$port","REG_SZ")
    messagebox("Printing on $printer$def",$uname,0,$timeout)
  endif
endif
; ----------------------------------------------------