Display web attachments in Notes client RTF

Is there a way to display attachments that were uploaded from a web browser in a RTF when viewed in the Notes client? The files are stored at the bottom of the form when viewed in Notes. Alternately, links to the attachments would be acceptable.

Thanks

Subject: Display web attachments in Notes client RTF

I found the following response in one of the posts in the forum…

The file upload control feature provided in Notes is used to attach files to a document from the web. But it ends up placing the attachment at the bottom of your document instead of in a rich text field. (Those of us who have been using Notes for a long time know this as a “V2 Attachment”). Here is some script that will take the attachment and place it into a rich text field. Place this code into your web query save agent for the form being created.

Sub Initialize

Dim s As New notesSession

Dim doc As notesDocument

Set doc = s.documentContext

Call WebMoveAttachment(Doc, “”)

End Sub

The subroutine “WebMoveAttachment” does the work of moving the attachment to the rich text field.

Function WebMoveAttachment(doc As notesDocument, Byval moveToFieldName As String)

’ This function moves a file attached via the Web with the File Upload Control to a rich text field.

Dim s As New notesSession

Dim tempDir As String

Dim v2FileNames As Variant

Dim i As Integer

Dim attachedFile As notesEmbeddedObject

Dim filePath As String

Dim rtItem As notesRichTextItem

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 & ""

’ Get the names of all the attachments (1 or more)

v2FileNames = Evaluate(“@AttachmentNames”, doc)

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))

     filePath = tempDir & v2FileNames(i)

     ' Save the file on the server

     Call attachedFile.extractFile(filePath)

     ' Create the rich text item and re-attach the file

     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(2)

     Else

        Set rtItem = New notesRichTextItem(doc, moveToFieldName)

     End If

     Call rtItem.embedObject(1454, "", filePath)

     ' Delete the file(s) from the server file system

     Kill filePath

  End If

Next ’ Move on to the next file name

End Function

Important: This function requires the ability to run UNRESTRICTED agents on the server. This is because the agent interfaces with the file system, which required unrestricted access.

Subject: RE: Display web attachments in Notes client RTF

Thanks for both the explanation and the code!

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