I have an agent that makes use of the LotusScript DIR$ function so that attachment(s) can be detached from documents that reside in the database. I keep getting an error message saying “Object Variable not set” which seems to point to the line where I make a call to a sub-routine function called “DetachToDir”. This agent is supposed to go through all the documents in the database and see if the target directory (to store the attachments in) exists and if does not then create it. The name of the target directory is captured from a profile document’s concatenated fields. Then the agent should do the same for the attachment(s).
I got some partial help from this forum but still experiencing problems. I am not so proficient in LotusScript … Any suggestion(s) as to how this can be fixed?
Here is something that I put together to deal with the collection of documents and detach the embedded object that resides in it:
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim object As NotesEmbeddedObject
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Dim ProfileDoc As NotesDocument
Dim downloadfolder As String
Dim NewLine As String
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Dim profileAttach_ServerName As NotesItem
Dim profileAttach_Directory As NotesItem
Set ProfileDoc = db.GetProfileDocument(“Extraction Settings”)
Set profileAttach_ServerName = ProfileDoc.GetFirstItem(“Attach_Directory”)
Set profileAttach_Directory = ProfileDoc.GetFirstItem(“Document_SubDirectories”)
On Error Goto Error_Handler ’ Error Handler
NewLine = Chr(10) & Chr(13) ’ For the error handler
downloadfolder = profileAttach_ServerName.Text & profileAttach_Directory.Text 'assign to downloadfolder variable value of 2 concatenated fields from the Extraction Profile document
If profileAttach_ServerName Is Nothing Or profileAttach_Directory Is Nothing Then
Msgbox “… one of more of the expected items in the Extraction Settings Profile is null.”
End If
If Dir(profileAttach_ServerName.Text + profileAttach_Directory.Text,16)=“” Then Mkdir(profileAttach_ServerName.Text + profileAttach_Directory.Text)
Print “************************************************************************”
For i = 1 To collection.Count
Set doc = collection.GetNthDocument( i )
filen=Evaluate(“@AttachmentNames”,doc)
antalfiler=Evaluate(“@Attachments”, doc)
If Dir(profileAttach_ServerName.Text + profileAttach_Directory.Text ,16)=“” Then Mkdir (profileAttach_ServerName.Text + profileAttach_Directory.Text )
Print Str(i)+" (“+Str(collection.count)+”)"
If antalfiler(0)>0 Then
For filecounter=0 To antalfiler(0)-1
x=x+1
Print ( filen(filecounter))
Set Object = doc.GetAttachment( filen(filecounter) )
If ( object.Type = EMBED_ATTACHMENT ) Then
fileCount = fileCount + 1
If Dir(downloadfolder + "\"+ filen(filecounter))="" Then
extrachar=""
Else
extrachar=Left(doc.universalid,4)+"---" 'in case attachment with same name exists in several documents
End If
Call DetachToDir(doc, downloadfolder)
End If
Next filecounter
End If
Next
Msgbox Str(fileCount ) + " Attachments were detached to the Attachments folder located in your Home directory " + downloadfolder + " on " + Format(Now(), “Long Date”) +“.”
Finished:
If filenum% > 0 Then
On Error Resume Next
Close filenum%’ Close the file
End If
Exit Sub
Error_Handler:
ErrorString = “The following error has occurred:” & NewLine
ErrorString = ErrorString & "Line number: " & Str(Erl) & NewLine
ErrorString = ErrorString & "Error number: " & Str(Err) & NewLine
ErrorString = ErrorString & "Description: " & Error$ & NewLine & NewLine
ErrorString = ErrorString & “Would you like to continue processing?”
If Messagebox (ErrorString, 4 + 16, “An error has occurred”) = 7 Then
Goto Finished’ Because the ‘No’ button was clicked…abort processing
End If
End Sub
Function DetachToDir( doc As NotesDocument, downloadfolder As String) As Boolean
DetachToDirectory = False
If doc.HasEmbedded Then
Set body = doc.GetFirstItem(“Body”)
Forall object In body.EmbeddedObjects’Get attachments in the document
If object.Type = EMBED_ATTACHMENT Then
filepath$ = downloadfolder & "\\" & object.Source
If Dir$( filepath$) Then
Call object.ExtractFile(filepath$)
DetachToDirectory = True
End If
End If
End Forall
End If
End Function
'If Dir(downloadfolder + Format(Now(), “Long Date”) +“"+ filen(filecounter))=”" Then
'Call object.ExtractFile (downloadfolder+“"+ Format(Now(), “Long Date”) +”"+extrachar+ filen(filecounter) )
'If Dir(profileAttach_ServerName.Text + profileAttach_Directory.Text + Format(Now(), “Long Date”) ,16)=“” Then Mkdir (profileAttach_ServerName.Text + profileAttach_Directory.Text + Format(Now(), “Long Date”) )