Notes Ninjas Notes Ninjas
Lotus Notes hints, tips, error messages
Composing a document that has an attachment already inserted

Hint Short Cut: Add this to your code & documentation to help you find this page.
http://www.notesninjas.com/#NewDocWithAttach or http://www.notesninjas.com/A555F9/nn.nsf/ByAlias/NewDocWithAttach


Customer wanted to create a new document that has a document attached automatically.





The code I used was:

In the QueryOpen event of the form I put this code which extracts the file required in to

the C:/Temp directory.

     If source.isnewdoc Then
         Dim db As notesdatabase
         Dim s As New notessession
         Set db = s.currentdatabase
         Dim v As notesview
         Set v = db.getview("Attachments")
         If v Is Nothing Then
              Print "Cannot find view ""Attachments"" (postopen)"
              End
         End If
         
         Dim doc As notesdocument
         Set doc = v.getdocumentbykey("DefaultAttachment", True)
         If doc Is Nothing Then
              Print "Cannot find doc "DefaultAttachment" (queryopen)"
              End    
         End If
         
         dirName$ = "C:\Temp"          
         On Error Goto ErrHdlr          
         attr% = Getfileattr(dirName$)
         Print attr%
         If attr% And 16 = 0 Then
              Print "Doh! " & dirName$ & " is not a directory! "
              End
         End If
AfterDirCheck:
         
         'Dir definitely exists"
         
         Dim TemplateItem As Variant
         Set TemplateItem=Doc.GetFirstItem("Body")
         i=0
         If (TemplateItem.Type = RICHTEXT ) Then
              Forall o In TemplateItem.EmbeddedObjects
                   If ( o.Type = EMBED_ATTACHMENT ) Then
                        Call o.ExtractFile ( dirName$ & "\" & o.source)
                        i=i+1
                   End If
              End Forall    
         End If    
         
         
    End If
    End
   
Errhdlr:
   
   
    If Err = 53 Then

          ' Directory doesn't exists so create it
         Mkdir dirName$
         Resume AfterDirCheck
    End If
    Msgbox "Quey Open Error " & Err & "   " & Error
    End

   

 

In the Post Open I put this @fuction language:

@Command([EditGotoField];"body");
@Command([EditInsertFileAttachment] ; "C:\\Temp\\DocAdam.doc")

 

Notes on the code above:

The query open assumes the file is stored in the body field of a keyword document.

The query open makes sure the directory exists and creates it if it doesn't.

Enhancements:

The filename extracted could be stored in an environment variable for the postopen event to use.

A query close could be written to remove or "kill" the file afterwards.

 

 

 

 

 Jump to:     Notes Tips
    Lotus Notes Index 
  Register for Tips
Previous Lotus Notes Tip 
    Next Lotus Notes Tip
Your company logo here?
Adam Foster
Free Cash Making Web Site
Free Revenue Earning Blog Site
 Free Revenue Earning Newsletter Site   
Free Search Pages Free Web stats
Free Contact Us page
Cheap .com domain names
 Need a website or 
   Lotus Notes
  problem solved?
Adam Foster
man and boy by Tony Parsons
British book of the year 2000. Great book, hard to put down, but I nearly threw it away half way through. You'll know why when you read it.
See at: Amazon.co.uk | Amazon.com |