Object variable not set?

Hi everyone,

I have an agent that runs on selected document to have their attached file extracted to a directory who’s value is stored in a profile document. I keep getting an error message telling me that a variable object is not set. I’ve rechecked the code and everything seems ok. Can anyone spot the problem? Any help would be appreciated. My debugger stops at the line that has the following code:

downloadfolder = profileAttach_ServerName.Text & profileAttach_Directory.Text

Here is the agent’s script code:

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

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

Set profileAttach_Directory = ProfileDoc.GetFirstItem("Attach_Directory")







‘downloadfolder = "H:\” & “Attachments"

downloadfolder = profileAttach_ServerName.Text & profileAttach_Directory.Text

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 + Format(Now(), "Long Date") ,16)="" Then Mkdir (profileAttach_ServerName.Text + profileAttach_Directory.Text + Format(Now(), "Long Date") )

	

	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 + Format(Now(), "Long Date")  +"\"+ filen(filecounter))="" Then 

					extrachar="" 

				Else 

					extrachar=Left(doc.universalid,4)+"---" 'in case attachment with same name exists in several documents

				End If

				Call object.ExtractFile (downloadfolder+"\"+ Format(Now(), "Long Date") +"\"+extrachar+ filen(filecounter) )

			End If

		Next filecounter

	End If

	

Next



Msgbox Str(fileCount ) + " Attachments were detached to the Attachments folder located in your Home directory (H:\Attachments) on " + Format(Now(), "Long Date") +"."

End Sub

Subject: Object variable not set ???

This error would occur if the profile document does not contain an expected field, ie: Attach_ServerName or Attach_Directory.

After getting handles to the above fields you might want to add additional code to handle the case of the items not found.

Example:

If profileAttach_ServerName Is Nothing Or profileAttach_Directory Is Nothing Then

’ … one of more of the expected items is null

End If

Also I strongly recommend adding proper error handling to your code. Have a look at this post which I revised someone’s code and added the error handling:

http://www-10.lotus.com/ldd/nd6forum.nsf/0/2246ea1bef82bf92852571ca0064fadc?OpenDocument

Alex

Subject: RE: Object variable not set ???

Hi Alex,

thanks for the help. Greatly appreciated! I adapted the sample code you gave me a link to and there is now something that I don’t understand …why can’t the agent create the target directory as specified by the code? Is it the date format to be used by te Operating System (Win XP)?

Here is the error message:

The following error has occurred:

Line number: 55

Error number: 4005

Description: Notes error: File cannot be created (c:\Program Files\Lotus\Notes\Data\Attachments Extract\Tuesday, January 29, 2008\798174.tif)

Would you like to continue process?

Here is the adjusted code:

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

’ Error Handler

On Error Goto Error_Handler



NewLine = Chr(10) & Chr(13) ' For the error handler



'downloadfolder = "c:\Program Files\Lotus\Notes\Data\Attachments Extract"

downloadfolder = profileAttach_ServerName.Text & profileAttach_Directory.Text

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 + Format(Now(), "Long Date") ,16)="" Then Mkdir (profileAttach_ServerName.Text + profileAttach_Directory.Text + Format(Now(), "Long Date") )

	

	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 + Format(Now(), "Long Date")  +"\"+ filen(filecounter))="" Then 

					extrachar="" 

				Else 

					extrachar=Left(doc.universalid,4)+"---" 'in case attachment with same name exists in several documents

				End If

				Call object.ExtractFile (downloadfolder+"\"+ Format(Now(), "Long Date") +"\"+extrachar+ filen(filecounter) )

				'Call object.Remove 'delete the attachment from document

				'Call doc.Save( True, False )

			End If

		Next filecounter

	End If

	

Next



Msgbox Str(fileCount ) + " Attachments were detached to the Attachments folder located in your Home directory (c:\Program Files\Lotus\Notes\Data\Attachments Extract) 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 

’ The ‘No’ button was clicked…abort processing

	Goto Finished

End If 

End Sub

Subject: RE: Object variable not set ???

It appears that you’re expecting Windows OS to automatically create the folder for you when you ask it to create a file; it doesn’t do this – you must create the folder yourself first.

Subject: RE: Object variable not set ???

Merci André. And thanks Alex.