Jump to content
roeselpi

printing without saving from an edit box

Recommended Posts

hello,

is it possible to have an edit box where you type in text and then press print and it will print the content of the edit box? it must however open the print dialog so i can chose which printer i would like to use, because i mainly use a pdf-printer that makes a pdf and then print it out of a pdf-reader software because it saves time.

example:

 

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("The Printing Issue", 310, 260, -1, -1)
$text = GUICtrlCreateEdit("", 24, 16, 265, 161, $ES_WANTRETURN + $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_MULTILINE)
GUICtrlSetData(-1, "This is the Text that i would like to edit, then read and finally print.")
$print = GUICtrlCreateButton("PRINT THE EDIT BOX CONTENT", 40, 200, 217, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $print
            $PrintData = GUICtrlRead($text)
            _FilePrint($PrintData)

    EndSwitch
WEnd

above will not work, so i was wondering if that is possible at all?

kind regards

Share this post


Link to post
Share on other sites

_WinAPI_PrintDlg will open the print dialog, not sure how to use the information it returns to print to a specific device selected though.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

i tried the _WinApi_PrintDlg but that is not really what i was looking for. the best was my initial approach above, but i always get an error, same goes for the actual help file

#include <File.au3>
#include <MsgBoxConstants.au3>

Local $sFilePath = FileOpenDialog("Print File", "", "Text Documents (*.txt)", $FD_FILEMUSTEXIST)
If @error Then Exit

Local $iIsPrinted = _FilePrint($sFilePath)
If $iIsPrinted Then
    MsgBox($MB_SYSTEMMODAL, "", "The file was printed.")
Else
    MsgBox($MB_SYSTEMMODAL, "", "Error: " & @error & @CRLF & "The file was not printed.")
EndIf

that will not work either. i always get an error message and after that the msgbox-error-notification

but: my initial aproach above did grab the text only just did not send it to the printer. so i guess it is not possible at all?

Share this post


Link to post
Share on other sites

Yes it is possible but with _FilePrint you need to save your data to a temp file then print it and delete file.  Something along those lines :

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("The Printing Issue", 310, 260, -1, -1)
$text = GUICtrlCreateEdit("", 24, 16, 265, 161, $ES_WANTRETURN + $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_MULTILINE)
GUICtrlSetData(-1, "This is the Text that i would like to edit, then read and finally print.")
$print = GUICtrlCreateButton("PRINT THE EDIT BOX CONTENT", 40, 200, 217, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $print
            FileWrite ("Temp.tmp", GUICtrlRead($text))
            _FilePrint("Temp.tmp")
            FileDelete ("Temp.tmp")

    EndSwitch
WEnd

Untested, but you will get the idea...

Share this post


Link to post
Share on other sites

@ Nine:

that gave me an idea. it may not be a great solution, but at least it works:

Case $print
            $PrintData = GUICtrlRead($text)
            FileWrite ("Temp.txt", $PrintData)
            FileOpen("Temp.txt",0)
            ShellExecute("Temp.txt", "", "", "")
            Sleep(1000)
            Send("^p")
            Sleep(1000)
            Send("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{ENTER}")
            Send("!{F4}")
            FileClose("Temp.txt")
            FileDelete("Temp.txt")

basically it writes the text into a temp.txt file and then opens that and then tells notepad to print it and to close notpad again. it is not the greatest solution but everything else failed so far. 

i can not seem to find any other solution.

the _FilePrint() keeps giving me a windows error about some programm not being declared by default. i do however have a default printer: i print directly to a pdf printing software that then gives me the option to print, or save the file with my real printer.

Share this post


Link to post
Share on other sites
Posted (edited)

@Nine gave you such a concise way to do this that is far less error prone than your workaround using notepad and send.

I'd suggest reconsidering it.  Working with application windows is much more error prone from the scripting perspective than files.  With application windows, there is so much that can go wrong, and you will come to find that your script will only 'work' a relatively low percentage of the time relative to working with files.  Of course you can over engineer a script that handles all the possible failure points and such, the three lines that Nine presented will always have a higher chance of working.   Just a warning...my two cents.

Just to throwout a few things:

Another window can become active, and eat your sends

Your window can exist but not have fully built out the control (the part you add text into)

Your window can exist, be fully painted and built, but not be enabled, or have a modal window which doesn't allow you to send/update the control

Your window can be in a 'ghost' state (the non-responsive status where the title becomes grey)

The script can send tab too quickly for the print modal, causing the wrong control to be focused on when you send enter

this list can go on and on.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

@jdelaney yes I know, I would much rather use _fileprint() as well, but 'my' windows will always give an error message for that. it just will not work and I can not quite figure out why. i do not quite understand why nobody has ever come accross this problem. does nobody want to be able to print from their script? i think, if you are working with the manipulation of a text, then printing is a vital requirement. i tried using the printing dialogue and that gave me the dialogue but did not print anything at all. either i was missing something, or it is not very universal in its usage. i do not know. 

however i do understand your 'concerns' and i guess you are right, but i know (so far) of no way to get the printing to work properly even with the help of a temp file. 

Share this post


Link to post
Share on other sites

hello again,

i am still pondering about the resolve of my printing issue (especially after a failed test on my alternative solution on a different computer system). i have added a screenshot of the error produced by the windows system that is currently running for the autoIT:

the errormessage (i will just translate, cause it is a german windows system) states: 

Quote

"This is the Text that i would like to edit, then read and finally print." could not be found. Please make sure you have spellt the name right and try again.

at first i was thinking that it is just hard luck and printing would not work until i started thinking about it a little more:

if the sciTE editor and / or even a little programe like notepad or the enhanced version notepad2 can print, well why can my script not print?

if i print from sciTE editor and/or notepad (no matter if a saved document or an unsaved document) it works perfectly with a printing dialogue poping up where i have the choice of printers and the default one is selected. when i then press print, it prints just like it should do.

that gets me thinking about what may be wrong in autoIT or in my script or anywhere else for that matter. there just must be a way to print saved or unsaved data out of a script or compiled exe file. 

i remember from many years back where i was looking for an organizer software and stumbled across otak-pim and it stood for "otak - personal information manager" and the author of the program back then had been asked why there was no printing capability and that many told him that pim did not stand for personal information manager but rather more for "printer is missing". i guess the programer must have stumbled over simular problems back when he created his software.

that said, i guess there is a way to print, the question is just how it can be done?

after having read old topics about printing and not being to find the right solution (so i think) i now would like to focus my attention to the last missing feature in my script: the printing issue. it is really dumb if you are using an editbox and can not print it without needing the help of other software that can print. that is quite pointless from my point of view, so i ask/beg and grovel for any help that might get me get the script to print like any good script that works with text should be able to.

help please.

printing-issue.png

Share this post


Link to post
Share on other sites

Didn't you notice that the name of the function _FilePrint() contains the term "file"?

Have you looked at the help about this function?

Do you invoke it by passing a file name?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

yes i tried writing a file first but that only gets me a different error:

translated: 

Quote

There is no default program for this action. Install a program to do so, or tell the system which the default program is if it is installed already.

so that did not bring any good results either. i had tried that before.

printing-issue2.png

Share this post


Link to post
Share on other sites
2 minutes ago, roeselpi said:

There is no default program for this action. Install a program to do so, or tell the system which the default program is if it is installed already.

This pretty clear: no program is currently selected to "print" a file with extension .txt

Google "file association" with the verb "print".


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Yes, provided .txt is associated with name "txtfile" but its "textfile" in some setups...


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

after adding above registry key the fileprint did work but only if i change the standard program to open *.txt files to notepad but i currently use notepad2.exe as a portable version: notepad2 homepage and of course that did then not work anymore to open *.txt files. so now the big question would be:

can i create an imanginary ending like *.xyz and then via the printscript have a registry setting written into windows without the user being informed that it was created and have a check run if that entry exists and if not then install that registry key? 

then i could if i use _FilePrint() then i could use the temp file tmp.xyz to be printed by using the old standard notepad.exe

that then would be again a fairly universal solution but if that would not work, then it also is not a real universal solution. 

on the notepad2 homepage, there is a sourcecode version of notepad2 which was written in c (i think) and included is a file called Print.cpp. after looking at it for a while i noticed that the man who wrote notepad2 used code from the sciTE editor for printing and that made me wonder if that is not somehow adaptable?

printing a file that is not saved yet with a standard printing dialogue would be my goal. it must be possible to create a printing instrument that prints like any other windows program would print no questions asked. 

Share this post


Link to post
Share on other sites
Posted (edited)
25 minutes ago, roeselpi said:

can i create an imanginary ending like *.xyz and then via the printscript have a registry setting written into windows without the user being informed that it was created and have a check run if that entry exists and if not then install that registry key? 

Yes.  Create first the .xyz as a class, then associated it with a class type. Check how .txt is associate with txtfile, do the same with .xyz.  Then create new key for the association and add the print command.  Again look how txtfile has been created and do the same.

 

Edited by Nine

Share this post


Link to post
Share on other sites

hi, 

after looking at a list of file extensions on the web, i decided to test the extension *.zzz (it does not seem to be used at the moment by any other program) and did a lot of testing with the registry and have come up with following solution:

 

Case $print

Global $tempfile = "zprint.zzz"

    RegWrite("HKEY_CLASSES_ROOT\.zzz", "", "REG_SZ", "zzzfile")
    RegWrite("HKEY_CLASSES_ROOT\.zzz", "Content Type", "REG_SZ", "text/plain")
    RegWrite("HKEY_CLASSES_ROOT\.zzz", "Perceived Type", "REG_SZ", "text")
    RegWrite("HKEY_CLASSES_ROOT\zzzfile", "", "REG_SZ", "Text Document")
    RegWrite("HKEY_CLASSES_ROOT\zzzfile\shell", "", "REG_SZ", "")
    RegWrite("HKEY_CLASSES_ROOT\zzzfile\shell\print", "", "REG_SZ", "")
    RegWrite("HKEY_CLASSES_ROOT\zzzfile\shell\print\command", "", "REG_EXPAND_SZ", "%SystemRoot%\system32\NOTEPAD.EXE /p %1")

    Sleep(500)

$PrintData = GUICtrlRead($text)
    FileWrite ($tempfile, $PrintData)
    _FilePrint($tempfile)

    Sleep(500)

    RegDelete("HKEY_CLASSES_ROOT\.zzz")
    RegDelete("HKEY_CLASSES_ROOT\zzzfile")
    FileDelete ($tempfile)

now this code above works like it should but: i am now missing the possibility of choice which printer should be used. currently it uses the default printer and that is super but using a typical print dialog (dialogue) would be nice and strangely the helpfile(s) for _WinAPI_PrintDlg and _WinAPI_PrintDlgEx are both more like an overkill because it is showing off what it can do, whilst when i print from notepad2.exe i get the _WinAPI_PrintDlgEx version but with the "Everything" Radiobutton activated and 1 Copy only. 

i know i can edit the example still a bit, but currently it is not quite clear to me how to implement above code into a printing dialog. any ideas?

Share this post


Link to post
Share on other sites

Beware that .zzz might already exist. Prefer a "most probably unique" extension, (you aren't limited to 3-char legacy and you can use many dots), like ".zzz.MyOwn.Ext.For.Easy.Printing"


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@jchd okay, thanks for the advice. i will keep that in mind and try to come up with something better. but for testing it worked here on this computer. 

Share this post


Link to post
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

×
×
  • Create New...