October 18, 2007

Print selected documents and attachments from a view

I have been trying to figure out how to print a document and its attachments when marking it in a view. A colleague had an agent which did the works as long as the attachments only are word documents. But if you don't know if it is a word, pdf, rtf, jpg or any other attachment the code gets long, uggly and still you proabably haven't covered all possible cases. I have however found a solution thanx to Charles Robinson who shows how to shell a windows application to open the attachments.

I have used my existing code with this one and the result is an agent that can run on selected documents in a view, print the documents and each document's attachments. It is run through a view action.

The code is not ultimate at all and there are still some minor problems left like when no application gets associated with the attached file nothing is printed out and that acrobat reader doesn't close when done. Probably fixable, but I am satisfied for now.

As you can see below I have used a sleep command. This might have to be adjusted depending on many different things, but it is needed or else the documents and attachments competes who gets to the printer first and everything is unsorted.

Here's the code. Hope someone can enjoy it!

In Options add Uselsx "*lsxlc"

In Declarations add Charles Robinsons code:

Const SEE_MASK_NOCLOSEPROCESS = &H40
Const SEE_MASK_FLAG_NO_UI = &H400

Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type

Declare Function ShellExecuteEx Lib "shell32.dll"_
Alias "ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long

Declare Function TerminateProcess Lib "kernel32"_
Alias "TerminateProcess" (Byval hProcess As Long, Byval uExitCode As Long) As Long


In Initialize add the following code:

On Error Goto ErrorHandler

Dim SEI As SHELLEXECUTEINFO

SEI.cbSize = Len(SEI)
SEI.fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_FLAG_NO_UI
SEI.lpVerb = "print"
SEI.nShow = 1
SEI.hInstApp = 0
SEI.lpIDList = 0

Dim s As New NotesSession
Dim session As New LCSession
Dim db As NotesDatabase
Dim object As NotesEmbeddedObject
Dim dc As NotesDocumentCollection

Dim doc As NotesDocument
Dim downloadfolder As String

Dim extrachar As String
Dim filecounter As Integer
Dim filecount As Integer
Dim filen As Variant
Dim antalfiler As Variant

Dim i As Integer, x As Integer, y As Integer

Dim sFile As String

Dim uidoc As NotesUIDocument
Dim ws As New NotesUIWorkspace
Dim success As Variant

' Create a folder for temporary storage of files
downloadfolder = Environ("tmp")

Set db = s.CurrentDatabase

Set dc = db.UnprocessedDocuments

For i = 1 To dc.Count
Set doc = dc.GetNthDocument( i )

' Print document
Set uidoc = ws.EditDocument( False , doc , True , )
Call uidoc.Print(1)

' Get all attachments and print them
filen=Evaluate("@AttachmentNames",doc)
antalfiler=Evaluate("@Attachments", doc)

Call uidoc.Close
Delete uidoc

If antalfiler(0)>0 Then
For filecounter=0 To antalfiler(0)-1
x=x+1
Set Object = doc.GetAttachment( filen(filecounter) )
If ( object.Type = EMBED_ATTACHMENT ) Then
fileCount = fileCount + 1
If Dir(downloadfolder+"\"+ filen(filecounter))="" Then
extrachar=""
Else
' Extra character in case there are attachments with the same name
extrachar=Left(doc.universalid,4)+"___"
End If
Call object.ExtractFile (downloadfolder+"\"+extrachar+ filen(filecounter) )
End If
Next filecounter
End If

For y = 0 To filecounter-1
sFile = downloadfolder+"\"+extrachar+ filen(y)
SEI.lpFile = sFile
Call ShellExecuteEx(SEI)
Call session.Sleep(2000)
Next

' Delete all files
Call session.Sleep(5000)
For y = 0 To filecounter-1
sFile = downloadfolder+"\"+extrachar+ filen(y)
Kill sFile
Next

'Call TerminateProcess(SEI.hProcess, 0)

Next

Exit Sub

ErrorHandler:
msgbox Error$ & " on line " & Cstr(Erl) & " in " & Lsi_info(12)
Exit Sub




Technorati tags:
, , ,


1 comment:

Anonymous said...

Thanks this code really helped me with someone at my company wanting the same thing. Works Great.