PreviousNext…

Show ’n’ tell VII: automated printing

Show-n-tell Thursday logoI am going to move on from the self-pitying posts now. Dark days behind me, dark days ahead. So, I shall seek solace in code on t’website

For today the “t” in “SnTT” will be for Tuesday rather than Thursday. Do not fret! I am in the red when it comes to my SnTT posts, so another will be forthcoming on the proper day. Today’s post is a simple one, but something that needs some attention. Every now and again, I receive an email or a post about the Holy Grail in Notes client programming: the manipulation of print settings. Back in the r4.x days, I used to use the technique I outline in my old article, Automate printer settings (Landscape or Portrait sir?). However, with Win2000 / WinXP, and the more recent releases of Notes, this is a non-starter. The code uses an old Win32 API call which doesn’t fly nowadays.

So, how to address the thorny issue of dialog box manipulation in modern times? Notes still has a number of omissions when it comes to this sort of thing—especially when it comes to printing—so it’s down to hackers like you and I. Here’s the solution I use nowadays, but beware: I don’t actually like writing code like this. It’s pretty brittle, and there’s a lot of overhead in testing. Where you know your client base, i.e. the operating system and Notes client version, the risk is manageable. Where you’re releasing code for a wider audience, well, just take care.

We go back to our old friend, SendKeys, only this time, rather than using the old library highlighted in the afore-linked article, we use the Windows Shell Extension (WSE). By way of an example, here is some code that invokes a print dialog in Notes, and then manipulates it to print the current document in landscape, rather than portrait, mode:

	Dim WshShell As Variant
	Dim i As Integer
	
	Set WshShell = CreateObject("WScript.Shell")
	
	' // Print (CTRL-P)
	WshShell.SendKeys "^p"
	' // Tab round 13 times to the "Page Setup" tab (!)
	For i = 0 To 12
		WshShell.SendKeys "{TAB}"
	Next i
	
	' // Select the "Page Setup" tab (RIGHT ARROW KEY)
	WshShell.SendKeys "{RIGHT}"
	
	' // Select "Landscape" (ALT-A) & then OK to print
	WshShell.SendKeys "%a"
	WshShell.SendKeys "{ENTER}"

As you can see, we are issuing specific keystrokes, and it’s for that reason I regard the code as being somewhat less than robust—you just don’t know how the dialog is going to change from workstation to workstation—the code above works flawlessly on my WinXP machine running Notes 6.5.2, but your mileage may well vary. The technique is still useful, and using WSE is certainly far less verbose than the SendKeys libraries of yore!

For more on the WSShell object, google around: there are stacks of posts out there. If you do a lot of Lotusscript calls to Win32 APIs you should check it out. For example, registry tweaks are far simpler via the WSE.

Comments

  1. Nice one Ben! This one saved my bacon today!

    Why on earth its not built in (option - "print this form in landscape"), is beyond belief!Colin Williams#

Comments on this post are now closed.

About

I’m a software architect / developer / general IT wrangler specialising in web, mobile web and middleware using things like node.js, Java, C#, PHP, HTML5 and more.

Best described as a simpleton, but kindly. You can read more here.

";