About Duffbert...

Duffbert's Random Musings is a blog where I talk about whatever happens to be running through my head at any given moment... I'm Thomas Duff, and you can find out more about me here...

Email Me!

Search This Site!

Custom Search

I'm published!

Co-author of the book IBM Lotus Sametime 8 Essentials: A User's Guide
SametimeBookCoverImage.jpg

Purchase on Amazon

Co-author of the book IBM Sametime 8.5.2 Administration Guide
SametimeAdminBookCoverImage.jpg

Purchase on Amazon

MiscLinks

Visitor Count...



View My Stats

« Why I share my personal trials, struggles, and triumphs... | Main| Book Review - Cross Bones by Kathy Reichs »

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

Comments

Gravatar Image1 - I'm new to LotusScript. How am I supposed to execute this script above when I want to print emails with their attachments?
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

Gravatar Image2 - i am a new comer in lotus!Now i have a problem.
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??

Gravatar Image3 - This agent assumes you're already in the document, and that the code is part of a button Click event. It's not designed to run from an agent on selected documents...

Gravatar Image4 - Hi Duffbert,
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
stw

Gravatar Image5 - Dear Tom,

Your program is a great help to my assignment . Thanks very much. However, I have been tried many times to print out a tif file. I couldn't find the correct application to open this file. If I use Visio to open it, I will see the convert message pop-up. But, I hope not to see it. Do you have any better idea or can you guide me to a correct place to find out more information that I need to know? Thanks again for your helps.

BR,
Stephanie

Gravatar Image6 - The way I wrote this, the answer is yes... you'll need to be in the document in order to use the script.

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.

Gravatar Image7 - Hi, Stephanie... You might want to try having your TIF files open in some other application, such as Paint Shop Pro or some other graphic image program. That might give you a better option on trying to get a print message with no other intervention. But right off, I don't have any fast and easy answers...

Gravatar Image8 - hi duff,
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

Gravatar Image9 - Using the above code i can print attachments of .doc and .xls but i am unable to print .pdf files.Please help me..

Gravatar Image10 - Duffbert, thanks for the reply.
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

Gravatar Image11 - Hi .. My customer has a dedicated inbox and they want to be able to automatically print all emails, pdfs, spreadsheets, and documents attached. In printer some third party tool has been attached so that when ever docuement is send for printing, the driver will convert the document into tiff image and save it on some folder. Do you have code for that?
how to set default printer and assign place to save the file and print emails thro scheduled agents.

Gravatar Image12 - Hi .. My customer has a dedicated inbox and they want to be able to automatically print all emails, pdfs, spreadsheets, and documents attached. In printer some third party tool has been attached so that when ever docuement is send for printing, the driver will convert the document into tiff image and save it on some folder. Do you have code for that?
<br />how to set default printer and assign place to save the file and print emails thro scheduled agents.

Gravatar Image13 - No I don't... sorry.

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Want to support this blog or just say thanks?

When you shop Amazon, start your shopping experience here.

When you do that, all your purchases during that session earn me an affiliate commission via the Amazon Affiliate program. You don't have to buy the book I linked you to (although I wouldn't complain!). Simply use that as your starting point.

Thanks!

Thomas "Duffbert" Duff

Ads of Relevance...