Variable object no set

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

Subject: Variable object no set

The error apparently happens inside the sub DetachToDir (if the error handler says so). Put error handling to the sub too, to see which exact object is not set.

The code is quite long and I cannot tell for sure - but do you have Dir function bug handled somehow? I mean, there is a bug that if you call Dir function more than once, and it doesn’t find the thing it was looking for. The first time, it returns an empty string, every next time it throws an error. The solution is to catch that error and set it to 0 if it happens (Err=0)

Subject: Type mismatch error msg

I am now getting a error message saying “Type Mismatch” for line 46 where the code says “If Dir$( filepath$) Then” …

Is filepath$ a reserved command? I searched for it and found nothing.

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

DetachedToDirectory = False

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

				filepath$ = downloadfolder & "\\" & object.Source

				If Dir$( filepath$) Then

					Call object.ExtractFile(filepath$)

					DetachedToDirectory = True

				End If 

			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

’ Close the file

	On Error Resume Next

	Close filenum% 

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

Subject: RE: Type mismatch error msg

No, but Dir$ returns a string and If expects a boolean (or a non-zero integer for True/zero for False).

Subject: Variable object no set

In your “DetatchToDir” subroutine you are calling GetFirstItem to retieve a NotesItem called “Body”. You never check to see if body actually exists. I would suggest that you protect yuor forall loop with a test of

if not body is nothing then

… put forall loop in here

end if

HTH