Avatar of nftcadmins
Flag for Canada

asked on 

Lotus Script - Exporting Attachments and Folder Creation

What I have is a database with many attachments with the same name. Some in the same document or some with the same name but in two different documents. What I'm trying to do is export all of the attachments into a seperate folder based on the value of the number field "FileNumber". If any attachments with the same name exist in the same document the file name is appended with a "1".

 When trying to create a variable to cast a file number into a string I get a Type Mismatch error.
Dim FileNumberString As String
FileNumberString = CStr(FileNumber)

Below is the entire script

Sub Initialize
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim doc As NotesDocument
	Dim nextdoc As NotesDocument
	Dim dirpath As String
	Dim view As NotesView
	Dim fullpath As String
	Set db = session.Currentdatabase
	Set view = db.GetView("All Documents")
	Set doc = view.Getfirstdocument
	Dim num As Integer
	dirpath = "D:\PlantEng"
	If (Dir$ (dirpath, 16)="") Then
		MkDir dirpath
	End If
	While Not doc Is Nothing
		Set nextdoc = view.getnextdocument(doc)
		'--Loop through all attachments in document and detach into D:\PlantEng
		Dim rtitem As Variant
		Set rtitem = doc.GetFirstItem("attachments")
		'--Get the unique file number attached to the document
		Dim FileNumber 
		Set FileNumber = doc.GetFirstItem("FileNumber")

		'--Create a variable to cast a file number into a string
		Dim FileNumberString As String
		FileNumberString = CStr(FileNumber)
		'--Create a variable to store the FileNumber as a folder
		Dim fileNumberFolder As String
		fileNumberFolder = dirpath + "\" + FileNumberString

		'--Create a new folder based on the file number
		MkDir fileNumberFolder

		'--if array of embedded objects exist them detach all attachments into D:\PlantEng
		If IsArray(rtitem.EmbeddedObjects) Then
			ForAll o In rtitem.EmbeddedObjects
				If(o.type = EMBED_ATTACHMENT) Then
					If o.name <> o.source Then
						fullpath = fileNumberFolder & "\" & o.source & +1
					fullpath = fileNumberFolder & "\"& o.source
					End If
					Call o.ExtractFile(fullpath)
				End If
			End ForAll
		End If
	Set doc = nextdoc	
	End Sub

Open in new window

Lotus IBM

Avatar of undefined
Last Comment
kostas Gk

8/22/2022 - Mon