Printing the Document
Having worked through sending keystrokes to initiate the Save As dialog, printing shouldn't be very much different. If we look at the File menu we can see that 'P' does the print. In later versions of Windows Notepad CTRL-P is also a shortcut for printing, so we can use either ALT-FP or CTRL-P.
Remember it is always sensible to focus the app we want to send keystrokes to. The Notepad window title has changed ? it is no longer 'Untitled ? Notepad' as we have saved the document.
We could give the new window title, but what if we can't predict easily what the filename is going to be? Assuming no other Notepad windows are open we can use the wildcard as explained previously when discussing WaitWindowOpen. So we can do this:
SetFocus>notepad*
Press ALT
Send>fp
Release ALT
What happens when we send ALT-FP to Notepad depends on which version of Notepad you have. In earlier versions Notepad just went straight ahead and printed the document. But in later versions it displayed the Print dialog. So please try it manually first to decide which version you have. This reiterates the importance of working through the process manually to understand what needs to be automated.
If the Print dialog does not appear and Notepad just starts printing the document then skip directly to Step 2.
Step 1
If the Print dialog appears, we should wait for it to appear ? it's window title is 'Print' so we would do this:
WaitWindowOpen>Print
Now, we just want to print to the default printer. As it is the default it is already selected and we just have to press enter to activate the default 'Print' button:
Press Enter
And the Print dialog now disappears.
Step 2
What happens next? Well, if you look closely you will see that a new, smaller dialog, entitled 'Notepad' appears indicating that it is printing. With the small document we are dealing with here it will flash up rather quickly, but we need to be aware of it. Our macro needs to wait for this to disappear before we can finally close Notepad.
So after pressing enter on the print dialog we should wait for this little status window to appear, and then disappear again before we know Notepad is ready again for input. We can do this with a WaitWindowOpen followed by a WaitWindowClosed:
WaitWindowOpen>Notepad
WaitWindowClosed>Notepad
Don't be tempted to just use a WaitWindowClosed command on its own, as this pauses until the specified window is not present, and immediately after pressing Enter, the window may not yet be present ? your script's so fast that the little dialog has not had a chance to appear ? so the script would continue. We should wait for it to open and then wait for it to disappear. This is the only sure-fire method.