< Back

Document Subject: 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://#NewDocWithAttach or http://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.