Printing attachments in a Notes document via LotusScript...
Category Software Development
At work, I had a project involving a vanilla-type Notes app to store information about system backup and recovery procedures. The only requirement (that came late in the game) was to be able to print off the documentation to satisfy the auditors. Fine, except that most of the Notes documents have attachments, and they really want those to print off automatically without manually opening each one.
I trolled the discussion groups and came across some code (forgot the source now) that gave me most of what I needed to make this work. Their requirement was to be able to handle Word, Excel, Visio, Project, and .txt files (using Notepad). Here's the code I came up with that seems to work fine.
I'm sure y'all will let me know any improvements... You're pretty good about that... :)
-----------
Sub Click(Source As Button)
'This routine is set up to scan a document for attachments and then
print them out automatically based on their file
'extension and associated application.
Dim ws As New NotesUIWorkSpace
Dim session As New NotesSession
Dim dbThis As NotesDatabase
Dim uidocThis As NotesUIDocument
Dim docThis As NotesDocument
Dim embAttachment As NotesEmbeddedObject
Dim strFileUniqueID As String
Dim strAttachmentType As String
Dim strAttachmentName As String
Dim lngPrintDelay As Long
Set dbThis = session.CurrentDatabase
Set uidocThis = ws.CurrentDocument
Set docThis =uidocThis.Document
'This prints the actual Notes document prior to printing the
attachments.
Call uidocThis.Print(1)
strFileUniqueID = Format(Now, "yyyymmddhhmmss")
'By traversing all the item elements in the document, all attachments
should be snagged.
Forall i In docThis.Items
If i.type = Attachment Then
Set embAttachment = docThis.GetAttachment(i.values(0))
strAttachmentType = Right$(embAttachment.Name, 3)
strAttachmentName = "c:\temp\BRDocPrintAttachment" &
strFileUniqueID
Call embAttachment.ExtractFile(strAttachmentName)
Select Case Lcase(strAttachmentType)
'This routine prints txt files with the Notepad
application running under the /p print switch. It assumes that
'Notepad.exe is running in c:\Windows.
Case "txt"
Dim strNotepadLocation As String
Dim intTaskID As Integer
strNotepadLocation = "c:\Windows\Notepad.exe"
intTaskID = Shell(strNotepadLocation & " /p" &
strAttachmentName)
Kill strAttachmentName
'This routine prints Excel files. It assumes that Excel
is installed on the machine doing the printing.
Case "xls"
Dim ExcelApp As Variant
Dim ExcelDoc As Variant
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelDoc =
ExcelApp.Workbooks.Open(strAttachmentName)
Call ExcelDoc.PrintOut
Call ExcelDoc.Close(0)
Call ExcelApp.Quit
Set ExcelApp = Nothing
Kill strAttachmentName
'This routine prints Word files. It assumes that Word is
installed on the machine doing the printing.
Case "doc"
Dim WordApp As Variant
Dim WordDoc As Variant
Set WordApp = CreateObject("Word.Application")
Set WordDoc =
WordApp.Documents.Open(strAttachmentName)
Call WordDoc.PrintOut
Call WordDoc.Close(0)
Call WordApp.Quit
Set WordApp = Nothing
Kill strAttachmentName
'This routine prints Visio files. It assumes that Visio
is installed on the machine doing the printing.
Case "vsd"
Dim VisioApp As Variant
Dim VisioDoc As Variant
Set VisioApp = CreateObject("Visio.Application")
VisioApp.Visible = False
Set VisioDoc =
VisioApp.Documents.Open(strAttachmentName)
Call VisioDoc.Print
Call VisioApp.Quit
Set VisioApp = Nothing
Kill strAttachmentName
'This routine prints Microsoft Project files. It assumes
that Microsoft Project is installed on the machine doing the printing.
Case "mpp"
Dim ProjectApp As Variant
Set ProjectApp =
CreateObject("MSProject.Application")
Call ProjectApp.FileOpen(strAttachmentName)
Call ProjectApp.FilePrint(1)
Call ProjectApp.FileClose(0)
Call ProjectApp.Quit
Set ProjectApp = Nothing
Kill strAttachmentName
'And if the attachment doesn't have a recognized
extension, we tell the user to print the attachment manually.
Case Else
Msgbox "This routine doesn't recognize " +
strAttachmentType + " file types. Please print the attachment manually."
End Select
End If
End Forall
End Sub
At work, I had a project involving a vanilla-type Notes app to store information about system backup and recovery procedures. The only requirement (that came late in the game) was to be able to print off the documentation to satisfy the auditors. Fine, except that most of the Notes documents have attachments, and they really want those to print off automatically without manually opening each one.
I trolled the discussion groups and came across some code (forgot the source now) that gave me most of what I needed to make this work. Their requirement was to be able to handle Word, Excel, Visio, Project, and .txt files (using Notepad). Here's the code I came up with that seems to work fine.
I'm sure y'all will let me know any improvements... You're pretty good about that... :)
-----------
Sub Click(Source As Button)
'This routine is set up to scan a document for attachments and then
print them out automatically based on their file
'extension and associated application.
Dim ws As New NotesUIWorkSpace
Dim session As New NotesSession
Dim dbThis As NotesDatabase
Dim uidocThis As NotesUIDocument
Dim docThis As NotesDocument
Dim embAttachment As NotesEmbeddedObject
Dim strFileUniqueID As String
Dim strAttachmentType As String
Dim strAttachmentName As String
Dim lngPrintDelay As Long
Set dbThis = session.CurrentDatabase
Set uidocThis = ws.CurrentDocument
Set docThis =uidocThis.Document
'This prints the actual Notes document prior to printing the
attachments.
Call uidocThis.Print(1)
strFileUniqueID = Format(Now, "yyyymmddhhmmss")
'By traversing all the item elements in the document, all attachments
should be snagged.
Forall i In docThis.Items
If i.type = Attachment Then
Set embAttachment = docThis.GetAttachment(i.values(0))
strAttachmentType = Right$(embAttachment.Name, 3)
strAttachmentName = "c:\temp\BRDocPrintAttachment" &
strFileUniqueID
Call embAttachment.ExtractFile(strAttachmentName)
Select Case Lcase(strAttachmentType)
'This routine prints txt files with the Notepad
application running under the /p print switch. It assumes that
'Notepad.exe is running in c:\Windows.
Case "txt"
Dim strNotepadLocation As String
Dim intTaskID As Integer
strNotepadLocation = "c:\Windows\Notepad.exe"
intTaskID = Shell(strNotepadLocation & " /p" &
strAttachmentName)
Kill strAttachmentName
'This routine prints Excel files. It assumes that Excel
is installed on the machine doing the printing.
Case "xls"
Dim ExcelApp As Variant
Dim ExcelDoc As Variant
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelDoc =
ExcelApp.Workbooks.Open(strAttachmentName)
Call ExcelDoc.PrintOut
Call ExcelDoc.Close(0)
Call ExcelApp.Quit
Set ExcelApp = Nothing
Kill strAttachmentName
'This routine prints Word files. It assumes that Word is
installed on the machine doing the printing.
Case "doc"
Dim WordApp As Variant
Dim WordDoc As Variant
Set WordApp = CreateObject("Word.Application")
Set WordDoc =
WordApp.Documents.Open(strAttachmentName)
Call WordDoc.PrintOut
Call WordDoc.Close(0)
Call WordApp.Quit
Set WordApp = Nothing
Kill strAttachmentName
'This routine prints Visio files. It assumes that Visio
is installed on the machine doing the printing.
Case "vsd"
Dim VisioApp As Variant
Dim VisioDoc As Variant
Set VisioApp = CreateObject("Visio.Application")
VisioApp.Visible = False
Set VisioDoc =
VisioApp.Documents.Open(strAttachmentName)
Call VisioDoc.Print
Call VisioApp.Quit
Set VisioApp = Nothing
Kill strAttachmentName
'This routine prints Microsoft Project files. It assumes
that Microsoft Project is installed on the machine doing the printing.
Case "mpp"
Dim ProjectApp As Variant
Set ProjectApp =
CreateObject("MSProject.Application")
Call ProjectApp.FileOpen(strAttachmentName)
Call ProjectApp.FilePrint(1)
Call ProjectApp.FileClose(0)
Call ProjectApp.Quit
Set ProjectApp = Nothing
Kill strAttachmentName
'And if the attachment doesn't have a recognized
extension, we tell the user to print the attachment manually.
Case Else
Msgbox "This routine doesn't recognize " +
strAttachmentType + " file types. Please print the attachment manually."
End Select
End If
End Forall
End Sub



Comments
I created an agent, pasted the code in it, set target "on selected documents", and when I select a email and go to the Actions menu, where my new agent appears, and click on it, noting happens. Tried to run it through the debugger, and it goes thru the Initialize section, in which I don;t have anything.
Please help, cause I have a bunch of docs with attachments to print...
Thanks
Posted by Svet At 17:04:30 On 19/11/2005 | - Website - |
in a form,i place two fields:
file (Rich Text Lite),just allow attachment
extension(computed the file's extension)
now i want to compute the extension of the file i upload from the file field.How could i do??
Posted by Anson At 19:12:51 On 21/08/2006 | - Website - |
Posted by Duffbert At 17:53:19 On 19/11/2005 | - Website - |
instead of coding the extension into your code you could consult the registry to find the matching command. Typically there can be the OLE Verbs retrieved so you would be able to print anything. A bit of digging into the Windows 32 API is needed.
The following code is ripped from VB.NET, so it won't work in LS, but it gives you an initial idea...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
of.ShowDialog()
If of.FileName.Equals("") Then
Return
End If
Dim psi As ProcessStartInfo
Dim pf As Boolean = False 'print found
Dim cv As String = "" 'curren verb
Dim v As StringBuilder = New StringBuilder
psi = New ProcessStartInfo(of.FileName)
Dim waskannich As String
For Each waskannich In psi.Verbs
v.Append(waskannich)
v.Append(", ")
If waskannich.Equals("Print") Then
pf = True
cv = waskannich
End If
Next
If pf Then
psi.Verb = cv
psi.WindowStyle = ProcessWindowStyle.Hidden 'seems not to work with Word2003?
Dim p As Process
'p = Process.Start(psi)
Label1.Text = "Available Verbs: " + v.ToString + ": Print executed"
Else
Label1.Text = "Sorry, I can't print I only can: " + v.ToString
End If
End Sub
Hth
Posted by Stephan H. Wissel At 00:16:02 On 26/08/2005 | - Website - |
Your program is a great help to my assignment
BR,
Stephanie
Posted by Stephanie Tan At 06:27:30 On 19/10/2006 | - Website - |
And the Click event is one you'll find if you create an Action button for the form or if you do a Hotspot button on the form.
Posted by Duffbert At 10:56:44 On 20/11/2005 | - Website - |
Posted by Duffbert At 17:41:09 On 24/10/2006 | - Website - |
shall i need to add a button in the view where the documents are existing or i can create an agent and paste this code in that?Please help me.....
Thanks
Posted by Krishna At 00:25:23 On 13/06/2006 | - Website - |
Posted by Krish At 02:32:22 On 15/06/2006 | - Website - |
I just don't understand when the Button click event will trigger.
And do I need to open up every document which I need to print with the attachments?
Thanks
Svet
Posted by Svet At 09:18:50 On 20/11/2005 | - Website - |
how to set default printer and assign place to save the file and print emails thro scheduled agents.
Posted by pridhana At 03:36:45 On 21/05/2013 | - Website - |
<br />how to set default printer and assign place to save the file and print emails thro scheduled agents.
Posted by pridhana At 03:43:41 On 21/05/2013 | - Website - |
Posted by Thomas Duff At 04:56:58 On 21/05/2013 | - Website - |