Page 1 of 1 1
Topic Options
#201645 - 2011-02-23 11:30 PM Printer Dialog
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Hello everybody. Is there any way to to initiate the printer dialog, and print a file from kix? Have a kixforms that's generating a report in a richtextbox...but haven't found any solutions to how to print it. I found a couple of UDFs, but they just simply send a specified file straight to printer. Trying to make this as universal as possible.

-Shane

Top
#201646 - 2011-02-24 11:38 AM Re: Printer Dialog [Re: ShaneEP]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Not sure, but this might give a clue:

vb.net example

or:

vb example

Top
#201647 - 2011-02-24 03:00 PM Re: Printer Dialog [Re: BradV]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
It might also be doable by using the printui.dll. It's been like 6 years or more since I messed with it, but in the addprinter() udf, it uses it to add a printer using a ini file driver. In the text of the header, I think I put how to get all the options it can do.

Rob Vanderwoude has a good write up too: http://www.robvanderwoude.com/2kprintcontrol.php

The .net stuff might be doable too with the com interface to powershell. http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=199178#Post199178

Top
#201656 - 2011-02-25 07:50 PM Re: Printer Dialog [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Wow I never realized how absent this technology was. I cannot find any solution to this problem. Even the vb samples Ive found are using licensed objects (dont want to have to worry about installing a dll just to be able to print). I think I'm just going to try to query for installed printers and then build a kixforms form that looks similar to the normal Print dialog. user can then select which printer and i can send the job in the background.
Top
#201657 - 2011-02-25 11:14 PM Re: Printer Dialog [Re: ShaneEP]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Don't give up just yet. I'm working something that might be useful. I will talk to you on mon.
Top
#201658 - 2011-02-26 03:11 AM Re: Printer Dialog [Re: Allen]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Found this but have not played with any of it.
http://www.scriptingtraining.com/forum2/forum_posts.asp?TID=3840

The link to Rob's site does mention this as well

C:\Windows\System32
prncnfg.vbs
prndrvr.vbs
prnjobs.vbs
prnmngr.vbs
prnport.vbs
prnqctl.vbs
pubprn.vbs


Top
#201661 - 2011-02-26 11:28 PM Re: Printer Dialog [Re: NTDOC]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Yeah I've found how to do most of those (via wmi scirptomatic), the only problem is that none of the tools initiate the printer dialog. This is what I have made so far (although Allen has probably already worked out some magical solution). It dynamically creates a list of available printers. I've only been able to test it on WinXP Home SP3 though. So if any of you get it to work on anything else, please let me know.
 Code:
$RC=setoption("NoVarsinstrings","on")
$RC=setoption("NoMacrosinstrings","on")

$System = CreateObject("Kixtart.System")

SelectPrinterForm()

Function SelectPrinterForm()
   $defprinter = Split(readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),",")[0]
   $objWMIService = GetObject("winmgmts:\\"+@WkSta+"\root\CIMV2")
   $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL")
   $x=0
   for each $colitem in $colitems
      $x=$x+1
   next
   Global $printers[$x-1]
   $x=0
   for each $colitem in $colitems
      $printers[$x]=$colitem.Name
      $x=$x+1
   next
   $PrinterSelectGroupHeight = (UBound($printers)+1)*30+40
   $PrinterSelectFormHeight = (UBound($printers)+1)*30+170
   $PrinterSelectform = $System.Form()
   $PrinterSelectform.Width = 400
   $PrinterSelectform.Height = $PrinterSelectFormHeight
   $PrinterSelectform.Text = "Print"
   $PrinterSelectTabs = $PrinterSelectForm.Controls.Add("TabControl")
   $PrinterSelectTabs.Height = $PrinterSelectForm.Height-50
   $PrinterSelectTabs.Width = $PrinterSelectForm.Width-20
   $PrinterSelectTabs.Center()
   $PrinterSelectTab = $PrinterSelectTabs.TabPages.Add("General")
   $PrinterSelectGroup = $PrinterSelectTab.Controls.Add("GroupBox")
   $PrinterSelectGroup.Text = "Select Printer"
   $PrinterSelectGroup.Height = $PrinterSelectGroupHeight
   $PrinterSelectGroup.Width = $PrinterSelectTabs.Width-30
   $PrinterSelectGroup.Top = 10
   $PrinterSelectGroup.Left = 10
   $top = 0
   For $x=1 to UBound($printers)+1
      $null = Execute('$printer'+$x+'=$PrinterSelectGroup.Controls.Add("ToolButton")')
      $null = Execute('$printer'+$x+'.Text=$printers[$x-1]')
      $null = Execute('$printer'+$x+'.BackColor="TransParent"')
      $null = Execute('$printer'+$x+'.FlatStyle=0')
      $null = Execute('$printer'+$x+'.BorderStyle=0')
      $null = Execute('$printer'+$x+'.Width=300') 
      $null = Execute('$printer'+$x+'.Height=30')
      $null = Execute('$printer'+$x+'.Cursor=16')
      $null = Execute('$printer'+$x+'.Top=20+$top')
      $null = Execute('$top=$printer'+$x+'.Top+10')
      $null = Execute('$printer'+$x+'.Left=$PrinterSelectGroup.Left+20')
      $null = Execute('$printer'+$x+'.OnClick="SetSelectedPrinter($printer'+$x+')"')
      if $printers[$x-1]=$defprinter
         $null = Execute('$printer'+$x+'.Icon=@ScriptDir+"\printer_def.ico"')
         $null = Execute('$printer'+$x+'.BackColor="silver"')
         $null = Execute('$printer'+$x+'.Pushed=1')
         $null = Execute('$selectedprinter=$printer'+$x)
      else
         $null = Execute('$printer'+$x+'.Icon=@ScriptDir+"\printer.ico"')
      endif
   Next
   $printbutton = $PrinterSelectTab.Controls.Add("ToolButton")
   $printbutton.Text = "Print"
   $printbutton.Top = $PrinterSelectGroup.Bottom+10
   $printbutton.Left = $PrinterSelectGroup.Left+80
   $printbutton.OnClick = "PrintHoursReport()"
   $printCancelbutton = $PrinterSelectTab.Controls.Add("ToolButton")
   $printCancelbutton.Text = "Cancel"
   $printCancelbutton.Top = $PrinterSelectGroup.Bottom+10
   $printCancelbutton.Left = $PrinterSelectGroup.Right-80-$printCancelbutton.Width
   $printCancelbutton.OnClick = "$$PrinterSelectform.Hide()"
   $PrinterSelectform.Center()
   $PrinterSelectform.Show()
   While $PrinterSelectform.Visible
      $Nul = Execute($PrinterSelectform.DoEvents)
   Loop
   Exit 0
EndFunction

Function SetSelectedPrinter($p)
   For $x=1 to UBound($printers)+1
      $null = Execute('$printer'+$x+'.BackColor=""')
   Next
   $selectedprinter = $p
   $selectedPrinter.BackColor = "silver"
EndFunction

Function PrintHoursReport()
   $testfile = @ScriptDir+"\test-print-file.txt"
   if not exist($testfile)
      $testfh = FreeFileHandle()
      $null = Open($testfh,$testfile,5)
      $null = WriteLine($testfh,"This is simply a test of the print form.")
      $null = Close($testfh)
   endif
   $prntr = $selectedprinter.text
   $null = SetDefaultPrinter($prntr)
   Sleep 1
   $objShellApp = CreateObject("Shell.Application")
   $objShellApp.ShellExecute($testfile,"","","print",0)
   Sleep 1
   $null = SetDefaultPrinter($defprinter)
   $PrinterSelectform.Hide()
EndFunction

Also...The .ico files are just little printer icons. Not sure how to upload them on here. But are any of you keen on how to place a windows icon in the form so there isnt the dependency on the .ico files?


Edited by CitrixMan (2011-02-26 11:39 PM)

Top
#201662 - 2011-02-27 04:33 AM Re: Printer Dialog [Re: ShaneEP]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
I've not played with KiXform in years but as I recall you can use base64.exe to create a text hash I think.

I'm sure one of the others that either continues to use it or was using it much more than I did can help you with it.

Who knows... maybe the author old Shawn the SheepShagger may stop by and help out

Top
#201663 - 2011-02-28 11:52 AM Re: Printer Dialog [Re: NTDOC]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Shane, do you need the printer dialog window, or just want to print? This link shows some extensive methods to use WSH to print:

Printing Documents in WSH

Regards,

Brad

Top
#201667 - 2011-02-28 07:27 PM Re: Printer Dialog [Re: BradV]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Ah thanks Brad! That article led me down a path that I think may just work. Check this out...
 Code:
Function IEPrintDialog($printdoc,optional $dialog)
   if $dialog<>1
      $dialog=2
   endif
   $objIE = CreateObject("InternetExplorer.Application")
   if @error=0
      $objIE.Visible = 0
      $objIE.Navigate($printdoc)
      Do
         Sleep 0.1
      Until $objIE.ReadyState = 4
      if $objIE.QueryStatusWB(6) >= 3
         $objIE.ExecWB(6,$dialog,2,0)
;         Do
;            Sleep 0.1
;         Until $objIE.PrintTemplateTeardown(pDisp)=1
         sleep 5
         $objIE.Quit
      else
         $objIE.Quit
         exit 1
      endif
   else
      exit @Error
   endif
   exit 0
EndFunction

The only problem Im having is having to sleep so enough time is allowed for the job to be sent before destroying the $ieobj. The article describes a way to check for the PrintTemplateTeardown event that signals the end of the print process, but I have no idea how to check an event of an object in kix. Maybe one of you knows vbs well enough to convert the syntax?

http://www.aspfree.com/c/a/Windows-Scripting/Printing-Documents-in-WSH/5/
http://msdn.microsoft.com/en-us/library/aa768346%28v=VS.85%29.aspx

Top
#201669 - 2011-02-28 10:55 PM Re: Printer Dialog [Re: ShaneEP]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Oh well... you got what I was working on. Sorry. I was out of town and didn't have access to a computer.
Top
#201670 - 2011-02-28 11:21 PM Re: Printer Dialog [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Good thought anyways! I don't know why I didn't think of using a different app to print off of. Still haven't found a way around having to use a sleep though (which I hate putting into a udf). Thought about doing a loop that checks for files in the spooler folder, but that wont work if you print to a unc'd printer.
Top
#201671 - 2011-03-01 05:44 AM Re: Printer Dialog [Re: ShaneEP]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Okay... try this.

 Code:
function IEPrint($document,optional $mode)
  dim $OLECMDID_PRINT, $OLECMDEXECOPT, $IE, $rc, $sc
  $OLECMDID_PRINT=6
  if $mode
    $OLECMDEXECOPT=1
  else
    $OLECMDEXECOPT=2
  endif
  if exist($document)
    $ie = createobject("internetexplorer.application")
    $ie.navigate ($document)
    While $IE.Busy and $IE.ReadyState <> 4 sleep 0.2 loop
    While $IE.Document.readystate <> "complete" Loop
    $IE.visible=0
    $sc = CreateObject("ScriptControl")
    $sc.language = "VBScript"
    $sc.timeout = 300000
    $sc.addobject('vbIE',$IE)
    $sc.addcode('vbIE.ExecWB ' + $OLECMDID_PRINT + ',' + $OLECMDEXECOPT + ',2,0')
    $sc.run
    $ie.quit
  else
    exit 2
  endif
endfunction


Last week when I initially was trying this, I was using very similar code as yours but the dialog would show up and then quickly disappear. This did not happen under vbscript. I ran out of time and never could figure out what was causing it until tonight. If the visibility setting is on the dialog box would just disappear under kix.

Then I started messing with the delay, and read that when the 3rd option is set to 2 like you have it, it is supposed to halt, but in kix it does not. However in vbscript it does, thus the code above.

Anyone have any idea why kix is handling these events differently than vbscript?

Top
#201672 - 2011-03-01 12:53 PM Re: Printer Dialog [Re: Allen]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Hi Allen,

When I tried your script, I got a dialog box asking if I wanted to open, save, or cancel. I selected open, but never got any printing.

I tried the notepad print in the reference URL and it worked as follows:

 Code:
Function NotepadPrint($objDocument)
   $objDocument.InvokeVerb("Print")
EndFunction
;
Function Get_Doc_Obj($strFolder, $strDocument)
   Dim $objShell, $objFolder, $colItems, $objItem
   $objShell  = CreateObject("Shell.Application")
   $objFolder = $objShell.NameSpace($strFolder)
   $colItems  = $objFolder.Items
   ;
   For Each $objItem In $colItems
      If $objItem = $strDocument
         $Get_Doc_Obj = $objItem
         Return
      EndIf
   Next
EndFunction


The above was in functions.kix

 Code:
Include "functions.kix"
Dim $strUser, $strFile, $rc, $strFolder, $objDocument
$strUser   = @USERID
$strFolder = "C:\Documents and Settings\" + $struser + "\Desktop\"
$strFile   = $strUser + "_info.ini"
;
$objDocument = Get_Doc_Obj($strFolder, $strFile)
$rc          = NotepadPrint($objDocument)


It briefly displayed the notepad as it printed. I'll have to keep looking.

Top
#201673 - 2011-03-01 02:08 PM Re: Printer Dialog [Re: BradV]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
@Brad... to see the print dialog try it with the $mode set to 1.

$rc=IEPrint(@scriptdir + "\test.htm",1)

Top
#201674 - 2011-03-01 05:11 PM Re: Printer Dialog [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
The dialog seems to stay open for me. Is the .Timeout essentially the same as my Sleep? If I hit cancel on the print dialog, the IE session remains open in my list of processes.
Top
#201676 - 2011-03-01 05:19 PM Re: Printer Dialog [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
The dialog on my function above stays open fine as well...if I have that sleep in there. if not the ie object gets destroyed before the print job is completely sent. the problem seems to lie in finding a way to loop and check for the completion of the print before destroying the object and terminating script.

In the vb example they do this...
 Code:
Set objIE = WScript.CreateObject("InternetExplorer.Application", "IE_")
...rest of printing stuff here...
Do While Not blnPrintingComplete
   WScript.Sleep 50
Loop

and
 Code:
Sub IE_PrintTemplateTeardown(pDisp)
   WScript.Sleep 200
   blnPrintingComplete = True
End Sub

Is there not a way to create the object and use it to watch for events like PrintTemplateTeardown in kix?

Top
#201678 - 2011-03-01 05:47 PM Re: Printer Dialog [Re: ShaneEP]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
The timeout has nothing to do with sleep. The Scriptcontrol times out with a message after 10 seconds no matter what script you are running, so I increased it to 5 minutes to give someone ample time to select a printer before the message appears.

The third option in ExecWB, which is 2, tells the dialog to wait until something happens, but Kixtart is not doing that. Vbscript does. If you try what I posted you will see it sits there until you hit print or cancel with no sleep needed.

Top
#201679 - 2011-03-01 06:38 PM Re: Printer Dialog [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
ah gotcha, that is weird.
Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 382 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.07 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org