Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site

   

Subscribe to OutlookExchange
Anderson Patricio
Ann Mc Donough
Bob Spurzem
Brian Veal
Catherine Creary
Cherry Beado
Colin Janssen
Collins Timothy Mutesaria
Drew Nicholson
Fred Volking
Glen Scales
Goran Husman
Guy Thomas
Henrik Walther
Jason Sherry
Jayme Bowers
John Young
Joyce Tang
Justin Braun
Konstantin Zheludev
Kristina Waters
Kuang Zhang
Mahmoud Magdy
Martin Tuip
Michael Dong
Michele Deo
Mitch Tulloch
Nicolas Blank
Pavel Nagaev
Ragnar Harper
Ricardo Silva
Richard Wakeman
Russ Iuliano
Santhosh Hanumanthappa
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath
 
 

Send all Incoming email to a printer Download scripts

 

The following script idea came from a question someone sent me, I thought it was a pretty cool idea and could see some practical applications so I decided to put an example together. Being able to print all the mail that arrives at a particular mailbox automatically from a server is not quite as easy at it first sounds. One solution is to use a outlook client side rule to do this, the drawback of this however is you need to have Outlook constantly open on a PC for this to work. The other option and the one I've used in this article is to use an event sink on the inbox to print all mail as it arrives.

 

Printing from the Exchange Server

To print from a script running on the Exchange server poses a few challenges heres one way I used to solve them . What this script does is writes the text body of the email message that fired the event sink to a simple text file. Firstly some simple code writes a little header for the print job that displays basic information about the email, such as who its is from and what time it was received after that it writes the text body of the message to the text file. The next part of the script runs a command line shell that uses the /p switch of notepad to open the text file created by the previous lines of script and automatically print that file to the default printer on the server. The thing to watch here is what context you have the Exoledb script host running under (if you haven't created your own Com object) you need to make sure the default printer for this user is set to the printer you want the email to print out.

How it works

I've used a two step approach to firing this event that adds a layer of abstraction for the Exchange event sinks. My event sink code looks as follows

<SCRIPT LANGUAGE="VBScript">

Sub ExStoreEvents_OnSave(pEventInfo, bstrURLItem, lFlags)

Stm = bstrURLItem
set WshShell = CreateObject("WScript.Shell")
strrun = WshShell.run ("c:\evtsink\auprint.vbs " & stm)
set WshShell = nothing
End Sub

</SCRIPT>

Main Script Auprint.vbs

set wshshell = Wscript.createobject("Wscript.Shell")
Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
   if I = 0 then
	inbstr = objArgs(I)
   else
	inbstr = inbstr & " " & objArgs(I)
   end if
Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set msgobj = CreateObject("CDO.Message")
msgobj.DataSource.Open inbstr, ,3 
fname = int((90000000 * Rnd) + 1)	' This generates a random number for the file name
fname = "c:\temp\" & fname & ".txt"
set wfile =  fso.opentextfile(fname,2,true)
REm Print Mail Header to file
wfile.writeline "*********************************************************************"
wfile.writeline "Email arrived From: " & msgobj.Fields("urn:schemas:httpmail:fromname")
wfile.writeline "Email Address: " & msgobj.Fields("urn:schemas:httpmail:fromemail")
wfile.Writeline "Email Recived At: " & msgobj.Fields("urn:schemas:httpmail:datereceived")
wfile.Writeline "Subject:  " & msgobj.Subject
wfile.writeline "*********************************************************************"
wfile.write msgobj.textbody
comex = "notepad.exe /p " & fname
strrun = WshShell.run (comex,1,TRUE)
wfile.close
fso.deletefile(fname)
set msgobj = nothing
The code is mostly straight forward I've used a random number generator to make sure that file name used is
unique.

Registering this script

For details on installing and registering this script see my previous article Using VBS Event Sink scripts with the Web Storage System. Or have a look in the ESDK search for regevent.vbs.   

Other options for Printing

If you want to use another option for printing then you could use a  direct copy to the printer but if you are planning on copying a text file directly to a printer you need to make sure you put some escape characters in the text file to setup the alignment and pagination of the print job. The advantage to using notepad is that the print job is aligned and prints properly via the notepad application. Download scripts


Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Stephen Bryant or Pro Exchange. OutlookExchange.Com, Stephen Bryant and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Copyright Stephen Bryant 2008