Print all attachments from documents

I received a request from a customer to be able to print out documents including all the attached files. A little search led me to the posting of Andrew Jones on OpenNTF which enables to print attachments with OLE Automation or a WindowsAPI.

In the posting it is assumed that attachments lay in a Rich Text field, in my case all the documents are being created from a web browser so Notes store the attachments in separate $File fields.

I was able to reuse most of the code, I only had to rewrite the follwoing sections:

 Dim rtitem As notesrichtextitem
 If doc Is Nothing Then Exit Sub
 If doc.HasItem (“Body”) = False Then Exit Sub
 Set rtitem = doc.GetFirstItem( “Body” )
 If ( rtitem.Type = RICHTEXT ) = False Then Exit Sub
 If Isempty(rtitem.EmbeddedObjects) Then Exit Sub
 Forall o In rtitem.EmbeddedObjects ‘ loops through all attachments
  If ( o.Type = EMBED_ATTACHMENT ) Then
   Call PrintAttachment (o)
  End If
 End Forall


 If doc.HasEmbedded Then
  Forall o In Doc.Items   
   ‘1048 means ATTACHMENT
   If o.Type = 1084 Then
    Call PrintAttachment (o)
   End If   
  End Forall
 End If

Note: the EmbeddedObjects property of the document would generate a list of embedded objects which you have to filter (links versus attachments)

If doc.HasEmbedded Then
 Forall o In doc.EmbeddedObjects
  … filter …
 End Forall
End If

In the PrintAttachment function I changed

fname = GetTmpDir + o.Source
Call o.ExtractFile ( fname )


Dim object As NotesEmbeddedObject
fname = GetTmpDir + o.Values(0) 
Set object = o.Parent.GetAttachment(o.Values(0)) 
Call object.ExtractFile(fname )

Since Notes has no doc.Print method I had to rewrite the code in the Agent that uses the ScriptLibrary from:

While Not(doc Is Nothing)
Call PrintAllAttachments(doc)
Set doc = collection.GetNextDocument(doc)


 While Not(doc Is Nothing)  
  ‘to print a document it has to be presented in the UI
  Set uidoc = workspace.EditDocument(False, doc)
  Call uidoc.Print
  Call uidoc.Close  
  Call PrintAllAttachments(doc)
  Set doc = collection.GetNextDocument(doc)

This opens the standard Print dialog for each document. A bit user-unfriendly. If you have suggestions to print documents on the background I am happy to receive them.


16 thoughts on “Print all attachments from documents

  1. ginny 2007-June-19 / 6:58 pm

    I’ve actually got this working… almost. I’m not an advanced user at all, though. I need to print file attachments for work automatically – we get up to 50-60 emails with attachments a day at a dedicated Lotus Notes inbox, so something to automate the process of printing all those attachments is urgently needed. Any and all attachments received at that address need to be printed.

    It’s now printing my Excel attachments when I run it, but I only need to print the first worksheet, not any of the others. I’m using the script library “as is” from the linked page with the addition of a simple agent that was in the comments immediately below the script library. Should I remove the simple agent? Modify it in some way? I will search around for a solution (which is probably very simple by your standards), but thank you for any help or pointers you can offer.

  2. Thilo Hamberger 2007-July-10 / 3:39 pm

    Call uidoc.Print( 1 ) does printing without user-interface. I also use Call uidoc.Close (True) cause otherwise you may end up with lots of windows open.

  3. Tina Miko 2007-July-25 / 9:33 pm

    This mostly works for me except for two things. First, it won’t close Adobe after I’ve printed. The second is when I select quite a few docs, sometimes I get error messages saying my attachment document couldn’t be found. I read somewhere that sometimes it tries to print a new attachment in a new document before it finishes the first and you end up with errors. I’m thinking if I can close Adobe after each print, maybe that will take care of it. I’ve been trying to research shellid thinking maybe I can somehow close it when I know the taskid but can’t find much out there. Thilo: thanks for the tip on the Close. I’d forgotten and had alot of windows open!

  4. quintessens 2007-July-28 / 11:36 am

    yes, adobe does not close correctly at this point. i bet there must be ways to close adobe, but this topic has no further attention from my point of view…

  5. Charis 2007-November-29 / 9:15 pm

    Oh this is so what I need too, but I have no clue as to how to actually apply it. I know it’s probably something like in Access where I write a macro and then have the system call and run the macro when a page is opened or a button is clicked but I just can’t seem to make the leap. I’m not a Lotus Notes programmer and so wish I could have Outlook where I used to be able to do this with a rule.

    Any and all help is greatly appreciated.

  6. sgm 2008-July-1 / 7:30 pm

    Will this work from a browser? The agent will be called from a button.

  7. quintessens 2008-July-2 / 11:43 am

    Uhm, let me know if you have tried it out =)

  8. Erik 2008-November-28 / 4:30 pm

    Hi Patrick,

    thx for the code for automatic printing. It works great 🙂

    Do you know if one of someone all ready found a solution for the adobe not closing problem?

    I’m going to google for it and will keep you posted if I find something.

    Hope to hear from you soon.

    gr. Erik

    • pridhana 2013-May-22 / 3:39 pm

      Could you please share the code for automatic printing,Thanks

  9. bobberator 2009-March-18 / 10:36 pm

    Dear all,

    I have trouble establishing an agent that shall do a similar task:
    I want to print a selection of emails in that way, that I want only the first page of each selected mail to be send to the printer.
    Is there a way, unfortunately, I cannot get that done.

    Thx in advance

  10. Tiotsop rousseil 2010-January-28 / 11:53 am

    Thank you for your help Patrick.

    There zqs q bug in my prgram because of

    If doc Is Nothing Then Exit Sub
    If doc.HasItem (“Body”) = False Then Exit Sub
    Set rtitem = doc.GetFirstItem( “Body” )
    If ( rtitem.Type = RICHTEXT ) = False Then Exit Sub
    If Isempty(rtitem.EmbeddedObjects) Then Exit Sub

    You sample help me.
    Sorry i m french speaker.

  11. Dale 2010-March-11 / 9:35 pm

    Hi Patrick! Thanks for your post here. I have a very similar request. My customer has a dedicated inbox and they want to be able to automatically print all emails, pdfs, spreadsheets, and documents attached. Do you have code for that? Thanks for your contributions to Lotus Notes Development.

  12. Quick Facts 2010-October-30 / 12:40 am

    You you should change the page subject Print all attachments from documents All about Lotus Domino Development (AaLDD) to more suited for your blog post you create. I loved the the writing even sononetheless.

  13. Adam Brown 2010-November-11 / 8:46 am

    I have used Andrew’s code on Lotus Notes 7, but to no avail. As a matter of fact, it possibly caused a few computers on my network to crash. Is this coding compatible for version 7? Making the changes above caused errors to come up. Plus I couldn’t see where I was supposed to insert the last change you suggested. Is there an idiots guide to this sort of thing that you can direct me to? Getting this agent to work would save me nearly an hour everyday.

  14. pridhana 2013-May-21 / 11:33 am

    Hi .. I have a very similar request. 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? Please help me in this

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s