Subject: RE: Display web attachments in Notes client RTF
I ran into problems with additional ‘ATT****’ $FILEs being generated with this agent code being executed in the WQS event, so I moved the code to the PostOpen and PostModeChange events in the client instead, and it works fine, (made a few modifications) no more garbage ATT**** $FILES.
Here’s the code:
Form declarations section:
Dim OKToEdit As Integer
Dim db As NotesDatabase
Dim i As Integer
Dim cntr As Integer
Dim attachedFile As notesEmbeddedObject
Dim filePath As String
Dim rtItem As notesRichTextItem
Dim item As NotesItem
Dim tempDir As String
Dim uidoc As NotesUIDocument
Dim uidocNew As NotesUIDocument
Dim doc As notesDocument
Dim v2FileNames As Variant
Dim docNew As NotesDocument
PostOpen and PostModeChange events:
Call WebMoveAttachment(Doc, Source, “Drawings”)
PostOpen and PostModeChange events:
Call WebMoveAttachment(Doc, Source, “Drawings”)
Function:
Function WebMoveAttachment(doc As notesDocument, uidoc As NotesUIDocument, Byval moveToFieldName As String)
' This function moves a file attached via the Web with the File Upload Control to a rich text field, Drawings
Dim s As New notesSession
Dim ws As New NotesUIWorkspace
v2FileNames = Evaluate("@AttachmentNames", doc)
' Get the names of all the attachments (1 or more)
tempDir = s.getEnvironmentString("Directory", True)
’ Put a trailing slash at the end of the directory if it is needed
If Instr(tempDir, "/") <> 0 And Right(tempDir, 1) <> "/" Then tempDir = tempDir & "/"
If Instr(tempDir, "\") <> 0 And Right(tempDir, 1) <> "\" Then tempDir = tempDir & "\"
Set item = doc.GetFirstitem("$FILE")
For i = Lbound(v2FileNames) To Ubound(v2FileNames)
If v2FileNames(i) <> "" Then ' Make sure it's a valid file name
Set attachedFile = doc.getAttachment( v2FileNames(i))
' Save the file on the server
filePath = tempDir & v2FileNames(i)
Call attachedFile.extractFile(filePath)
' delete the document attachment before re-attaching to the Drawings field.
Set rtItem = doc.getFirstItem(moveToFieldName)
Set object = rtitem.GetEmbeddedObject( v2FileNames(i))
If Not object Is Nothing Then
Forall o In rtitem.EmbeddedObjects
Select Case o.Type
Case EMBED_ATTACHMENT:
Call object.Remove
End Select
End Forall
Call doc.save (True, True)
End If
Call item.Remove ' Delete each $FILE field , it's been extracted now
' Create the rich text item and re-attach the file , (Drawings field)
If doc.hasItem(moveToFieldName) Then
Set rtItem = doc.getFirstItem(moveToFieldName)
' Add a couple of lines to the rich text field before re-attaching the file
' Call rtItem.addNewLine(1)
Else
Set rtItem = New notesRichTextItem(doc, moveToFieldName)
End If
If Instr(filepath , ".") <>0 Then ' if contains a period in the name (seperating extension from name), it's valid, so embed it
Call rtItem.embedObject(1454, "", filePath) ' won't get a dup name because the $File has already been deleted
Else
' Set item = doc.GetFirstItem("$FILE") ' weird ATT**** file, get rid of it
' Call item.Remove
End If
' Delete the file(s) from the server file system
Kill filePath
End If
Set item = doc.GetFirstItem("$FILE") ' Get next $FILE field if there is one.
Next ' Move on to the next file name
’ now close & reopen the UI document so it takes effect
If s.NotesBuildVersion >= 190 Then
rtitem.Update ' ND6 only
Else
Call doc.ComputeWithForm(True, False) ' caution, as this may erase some field values if you have @Db functions in formulas.
End If
docUNID = doc.UniversalID
’ Set uidoc = ws.CurrentDocument
doc.SaveOptions = "0" 'close document without save prompt
Call doc.Save(True, True) 'get rid of old doc
Call uidoc.Close(True) 'close and reopen doc so changes appear on table
Set docNew = db.GetDocumentByUNID(docUNID)
Set uidocNew = ws.EditDocument(True, docNew, False)
uidocNew.Document.RemoveItem("SaveOptions")
Delete uidoc
uidocNew.Document.RemoveItem("SaveOptions")
enditall:
End Function