thill.dk
July 31, 2010, 19:01:45 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: SMF - Nyt på thill.dk!
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Send all Incoming email to a printer (Event Sink & VB Script)  (Read 492 times)
admin
Thomas Thillerup
Administrator
Full Member
*****
Posts: 168


View Profile WWW
« on: December 10, 2007, 23:41:00 PM »

http://www.outlookexchange.com/articles/glenscales/icomprn.asp

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.
Logged

Cruel but unjust
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.176 seconds with 17 queries.