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.

Code:

'This code adapted from a 
VB code posted by Dev Ashish 
'http://www.mvps.org/access/api/api0018.htm
'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
Long

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

MEMBER FEEDBACK TO THIS TIP

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 SearchDomino.com 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.

  • This was first published in May 2004

    Dig deeper on LotusScript

    0 comments

    Oldest 

    Forgot Password?

    No problem! Submit your e-mail address below. We'll send you an email containing your password.

    Your password has been sent to:

    -ADS BY GOOGLE

    SearchWindowsServer

    Search400

    • iSeries tutorials

      Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

    • V6R1 upgrade planning checklist

      When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

    • Connecting multiple iSeries systems through DDM

      Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

    SearchEnterpriseLinux

    SearchDataCenter

    SearchExchange

    SearchContentManagement

    Close