Print all attachments using shellExecute in Windows

This script prints all attachments on a Notes doc's Body field. You do not need to know the attachment names ahead of time, or the app that needs to be invoked to print them.

View member feedback to this tip.

This script prints all attachments on a Notes document's Body field. You do not need to know the attachment names ahead of time, or the application that needs to be invoked to print them. The shellExecute command uses the Windows registry to determine the correct application to print the attachment.

This works with Notes Clients 4.6.7 and 5.0.11 running Windows 2000 or XP. It probably works with other Windows versions and Notes clients as well.

This code detaches attachments in the Body field for the current document and saves them on the user's hard drive in the c:notesdata directory.

Note: This code leaves these attachments on the user's hard drive. You will need to add a separate routine to delete them, because the shellExecute print process does not like you deleting the files while it is printing. I did not include code here to delete these temporary files.


'This code adapted from a 
VB code posted by Dev Ashish 
'Remember these declarations
Declare Function ShellExecute Lib 
"shell32.dll" Alias "ShellExecuteA" _ (Byval
hwnd As Long, Byval lpszOp As 
String, _ Byval lpszFile As String, Byval
lpszParams As String, _ Byval LpszDir 
As String, Byval FsShowCmd As Long) As

Declare Private Function 
GetDesktopWindow Lib "user32" () As Long

Private Const SW_HIDE = 0&

'You need to call the this sub from some
 other sub, like the Click or
Initialize sub. ' like this:
' Call PrintAttachment (doc) 

Sub PrintAttachment(doc As NotesDocument)
        Dim rtItem As NotesRichTextItem 
        Dim Scr_hDC As Long 
        Dim ret As Long 
        Dim file As String
        Set rtItem = doc.GetFirstItem("Body") 
        If Not rtItem Is Nothing Then 
                Forall o In rtItem.EmbeddedObjects 
                        file =  o.Name 
                        Call o.ExtractFile( "c:notesdata" & file )
                        Scr_hDC = GetDesktopWindow() 
                        ret = shellExecute
(Scr_hDC, "print",file ,Null ,"c:notesdata" ,SW_HIDE)
                        If ret <= 32 Then 
                                Msgbox "An error occurs 
while printing!" 

                        End If 
                End Forall 
        End If 
End Sub


This looks like it should work, but there are a couple of problems:

  • Detaching the file to c:notesdata is a bad idea because:

    1. The code should not assume that such a directory exists. Notes can be installed with different data paths.
    2. If a file with the same name already exists in the directory, this code is overwriting it. If the file is, for example, names.nsf, user data could be irretrievably lost.

  • The code should create a subdirectory in the windows "temp" directory (Environ("Temp")) instead of using a hardcoded path.

    The whole issue of when the files get deleted is left up in the air.

  • I know that there are Windows API calls that let you wait for a task to complete.

    Andre Guirard

    Do you have comments on this tip? Let us know.

    This tip was submitted to the tip exchange by member Gregg Bendtsen. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

  • Dig Deeper on LotusScript



    • Favorite iSeries cheat sheets

      Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...

    • HTML cheat sheet

      This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

    • Carol Woodbury: Security

      Carol Woodbury