Separate multiple attached files into individual notes documents

Use this agent code to break up the attachments into their own notes documents.
LotusScript


	Dim session As New NotesSession
	Dim db As NotesDatabase
	Set db = session.currentdatabase
	Dim doc As NotesDocument
	Set doc = session.DocumentContext	
	
	Dim x As Long
	Dim filenames() As String
	ReDim filenames(x) As String

	'create an array of all the filenames
	ForAll f In doc.items
		If f.name="$FILE" Then
			If filenames(0) = "" Then
				filenames(x) = f.values(0)
			Else
				x = x + 1
				ReDim Preserve filenames(x) As String
				filenames(x) = f.values(0)
			End If
		End If	
	End ForAll

	'create the attachment sub docs (splitting up the attachments)
	Dim y,z As Integer
	Dim adoc As NotesDocument
	Dim neo As NotesEmbeddedObject
	For y = 0 To UBound(filenames)
		Set adoc = db.Createdocument()
		Call doc.CopyAllItems(adoc, False)
		ForAll i In doc.items    
			If i.name<>"$FILE" Then Call adoc.RemoveItem(i.name)
		End ForAll
		adoc.form = "attachment"
		'INSERT THE OTHER STAMPING FIELDS
		Call adoc.save(True, True)

		'strip out the other attachments
		For z = 0 To UBound(filenames)
			If y = z Then
				'do nothing, leave this file
			Else
				'get it and remove it
				Set neo = adoc.Getattachment(filenames(z))
				If Not neo Is Nothing Then Call neo.Remove()
			End If
		Next z
		Call adoc.save(True, True)
	Next y

Written by fbrefere001

Posted by fbrefere001 on Thursday May 13, 2021