Showing posts with label File Attachments. Show all posts
Showing posts with label File Attachments. Show all posts

Domino: Read a file attachment without detaching

The function below shows how you can read a file attachment, in this case a text or csv file into a string without detaching it to disk.
Function getAttachmentData

 Dim doc As NotesDocument
 Dim stream As NotesStream
 Dim export As NotesDXLExporter
 Dim mime As NotesMimeEntity
 Dim txt As String
 Dim Start As Double
 Dim Finish As Double
 Dim fileData As String

 On Error Goto processError

 If Not dataDictView Is Nothing  Then Set doc = dataDictView.getDocumentByKey(|ARCHIVE CSV FILE| , True ) 

 If Not doc Is Nothing Then
  Set stream = session.CreateStream
  Set export = session.CreateDXLExporter
  'Convert the document to DXL
  export.setInput doc
  export.setOutput stream
  export.process
  'Process the export into a string file (as long as its less than 2GB)
  txt=stream.ReadText
  txt = Replace(txt, Chr(10), ||)
  'Find the  tags and read what's between them
  Start=Instr(1,txt,||)
  Finish=Instr(1,txt,||)
  fileData=Mid(txt,Start +10, Finish-Start-11)

  'Truncate the stream and write the filedata into it
  stream.Truncate
  stream.WriteText fileData

  'Setup a NotesMIMEEntity to decode the Base64 string
  Set mime = doc.CreateMIMEEntity(|MIMEEntity|)
  mime.SetContentFromText stream, |text/plain;charset=UTF-8|, ENC_BASE64
  mime.DecodeContent
  getAttachmentData = mime.ContentAsText
 End If

 Exit Function

processError:

 currentlog.LogError Err, |Error: | & Err & |: | & Error & | on line | & Erl & | in Agent: getAttachmentData|
 Exit Function

End Function