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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SendKeys

Status
Not open for further replies.

EliseFreedman

Programmer
Dec 6, 2002
470
GB
I am working on an application to allow a user to automatically print on a photocopier the desired amount of copies of a specified document. This task is complicated by the fact that the user needs the ability to alter the way in which the copies are produced, each copy should be stapled together. At present I am displaying the print dialog box. The user has to then manually select all the desired options - for example double sided printing, stapling etc. This is done by selecting a particular Tab on the dialog(which does not have a shortcut key) and then selecting options from various combo boxes/option buttons none of which have shortcut keys assigned to them.

Can I use SendKeys to capture the keystrokes used to select the printing options. I would like to be able to do this so that I can set up the document automatically for printing. If this is not possible, is there an alternative way

Thanks in advance

Elise
 
Can your photocopier accept command codes via an RS232/serial port?
 
Sure you can use SendKeys. I created a custom WinBatch app for a client doing just this. (WinBatch can be found at and is, in fact, the original source of the greatly paired-down Microsoft scripting language. I think you'll find it a *whole* lot easier to use than VBA.)

Note that SendKeysTo (as used in WinBatch) is case-sensitive and the leading tilde (~) is very important. The {TAB} is used to navigate from field to field within the dialog box, including the tabbed title (e.g., "General). {SPACE} is used to check/uncheck checkboxes. {DOWN n} is used to select an item within a dropdown dialog box.

If the tabbed title has the focus, you can use {RIGHT n} to place the focus on an adjacent tabbed title.

Sample:

SendKeysTo("~Print", "{TAB}{SPACE}{TAB 2}{DOWN 3}{ENTER}
 
>WinBatch...is, in fact, the original source of the greatly paired-down Microsoft scripting language

An interesting assertion...
 
Great. Prove me wrong.

I've been using WinBatch since '87 when Windows was at version 1.03. Interestingly, the Microsoft scripting language only showed up *many* years later. I think you'll discover, upon thorough investigation, that M$ and Wilson Windowware did, in fact, cut a deal for a limited version of WinBatch to be dispersed under M$ auspices.
 
>Great. Prove me wrong.

Oh, please...

Even according to Wilsonware, let alone other documented sources (e.g. PC Magazine):

a) Wilsonware didn't exist before 1989
b) First version of Winbatch didn't appear until 1991 (same year as VB 1.0) after Command Post (see point c), derived as it was from Command Post's menu languuge
c) Command Post was launched approximately 1989 for Windows 2.0 (which itself wasn't released until Fall 1987)

Given the above facts, and the lack of any counter-evidence whatsoever supporting almost anything you've said, I have to take all your claims with a generous pinch of salt

And even if M$ had distributed a limited addition of WinBatch at any point, this is a substantially
different scenario from the one you originally alleged, and with which I disagreed, of Winbatch being the original source of Microsoft's scripting language.

You might think I'm being pedantic, but I hate to see wild, innaccurate claims made in a professional forum where other people might read them and believe them.

Please note that this does not mean I disagree with the idea that Winbatch's SendKeys may be better than that of VBA's - the MS implementation of this is a horrible kludge that often causes unexpected results
 
Any way.....

Back to the original question, If you know the ascii control codes that the copier is expecting (see the users manual), you can send a series of ascii strings to the copier via the LPT1: port.

You would use an old qbasic command that is not documented in vba, but still worked in office 2000.

open "LPT1:" for output as #1
print #1 my_string_of_ascii
close #1
 
Can you give me more information on using Winbatch.

For example, I tried to run your example Jagdriver
Code:
SendKeysTo("~Print", "{TAB}{SPACE}{TAB 2}{DOWN 3}{ENTER}

I typed the above code into Winbatch Studio. However, when I attempted to run it, It came up with the following error
3066 Wrong Number of arguments in Function.

What am I doing Wrong
 
Just after posting the above, I realised part of my problem. I had missed out a " and a ) at the end

Code:
SendKeysTo("~Print", "{TAB}{SPACE}{TAB 2}{DOWN 3}{ENTER}")

Now, when I run the code, I get the following

Please Wait, Winbatch Processing. However, Nothing happens.

Please help -- I am rather confused
 
OK, strongm, I stand corrected. Thanks for the research.

Anyway, Elise, in error I failed to include the trailing ") to close the expression. You recognized this.

The deal is that the Print dialog box has to be open for this to work. If you right-click on SendKeysTo in WinBatch Studio and do a Keyword Lookup, you'll learn more. Also check what the HLP has to say about SendKeys.

Experiment! Try the code w/o the trailing {ENTER} so you can visually see how SendKeysTo is driving changes within the dialog box. The code I provided was merely a sample; it did not explicitly oder up any "Do it" (print) command. So no, I wouldn't expect you'd see anything.

WinBatch comes with very good tutorials. And, in lieu of filling up this forum with stuff that's not related to VB or VBA, I *highly recommend* the forum at the WinBatch.com site. You don't have to be a registered user (i.e., have purchased the product) to participate in the forum.

Or you can e-mail me directly. Good luck! > Chris
 
I used SendKeys from VB6 but you have to force focus to the operating window (another App) and I had to use a load app instruction to get the number used to force focus or I had to know the caption in the titlebar of the window. In the case of Outlook that varies with every folder you have displayed so you can't always predict it.

I have not done it in VBA yet but I just wanted to sendkeys to Excel from VBA in Excel and I am struggling.
 
This is not a WinBatch forum. As you have found, no application will work if you do not use it correctly. Unfortunately many subscribers to these forums do not bother to test their code or assume that something cannot be done because they cannot do it. The following refers to VBA. It is just meant to be an example of how to use Sendkeys. It works OK for me on my machine.

If you perform the process using the keyboard you will note that things do not happen instantly on screen - especially the initial opening of the dialog box where the action takes place. The main problem with SendKeys is that the code usually runs faster than Windows can carry out the required activity - this is especially so if manipulating an external (not the one running the code) application or opening a dialog box or menu. We therefore need to slow the action down by using Wait statements. SendKeys also has its own "Wait" argument True or False which needs to be used correctly. This is not always sufficient, so extra code is required. One example of using it set to True is when manipulating external applications that require extra time to carry out actions.

A big key to success in this area is to break down the process into small steps. It does need a bit of trial and error to get it right. Experience shows that operators are prepared to wait a bit longer if it works. Sometimes the speed of external applications varies so much that the job is impossible using SendKeys, when using API calls with KeyUp and KeyDown work better. This area is never an exact science.


'------------------------------------------
Sub SET_A3()
'- nb. assumes current setting is A4
Dim AltKey As String
Dim CtrlKey As String
Dim ShiftKey As String
Dim TabKey As String
Dim EnterKey As String
'-----------------
AltKey = "%"
CtrlKey = "^"
ShiftKey = "+"
TabKey = "{TAB}"
EnterKey = "~"
'------------------
'- open print dialog
SendKeys AltKey & "(FP)", False
'- delay 1 second
Application.Wait Now + TimeValue("00:00:01")
'- open printer properties
SendKeys AltKey & "(R)", False
'- tab to paper size
SendKeys TabKey, False
SendKeys TabKey, False
'- change A4 to A3
SendKeys "{UP}", False
'- Enter twice
SendKeys EnterKey, False
SendKeys EnterKey, False
End Sub
'----------------------------------------------


Regards
BrianB
Use CupOfCoffee to speed up all windows applications
================================
 
I wanted to open-up the format dialog > font tab and leave it there for the user to get the idea it is a font changing operation they then chose and close.

Unfortunately the dialog closes as soon as the sub exits.

but at least the SendKeys is working within Excel - it is just that the dialog is there so fleetingly it can be missed.

(sticks thimb in mouth and scrathes head).
 
If you mean my code, that is what the 2 'EnterKeys' at the end do. So you just need to leave off the end bit that changes A3 to A4. With a bit more work you could probably do the whole job.:-

'------------------------------------------
Sub OPEN_PRINT()
'- nb. assumes current setting is A4
Dim AltKey As String
Dim CtrlKey As String
Dim ShiftKey As String
Dim TabKey As String
Dim EnterKey As String
'-----------------
AltKey = "%"
CtrlKey = "^"
ShiftKey = "+"
TabKey = "{TAB}"
EnterKey = "~"
'------------------
'- open print dialog
SendKeys AltKey & "(FP)", False
'- delay 1 second
Application.Wait Now + TimeValue("00:00:01")
'- open printer properties
SendKeys AltKey & "(R)", False
End Sub
'----------------------------------------------


Regards
BrianB
Use CupOfCoffee to speed up all windows applications
================================
 
How do I get the user's input for the number of copies that they want to print when Im using SendKeys.

 
Have I missed the point entirely?

Surely the whole point of VBA is that there are much better ways of doing everything than using send keys?

If you are not confident enough to write the code from scratch why not record a macro? Even if it does not provide the worlds most perfect code its a good start.

To get the number of copies use a message box eg:
Copies:=msgbox ("How many coppies do you need?")
 
I am using sendkeys to allow me to select various printing options automatically including double sided printing and stapling
 
EliseFreedman,
As far as I know SendKeys is the only way to do that. However, is the printer the same every time for all users? If the model or driver changes, the required keys may change.

If the file is the same for all users, another possibility is that you can generate an output file with all the required settings (use Print to File and save is as outputFile.prn) and then you can send that file directly to the printer (using LPR or Ghostview [if it's a PS printer], etc). Btw, what type of printer is it (make/model, printer language [PS, PCL, etc]) and how is it connected (parallel, TCPIP, etc)?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top