Type mismatch error

I am 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. Don’t know why I getting this error. Can someone enlighten me?

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: Type mismatch error

What are you testing for with your “If Dir$( )”?

The expression after the “IF” needs to evaluate to a boolean TRUE or FALSE.

Subject: Type mismatch error

filepath is not a reserved word. If it was, the code would not compile correctly. But maybe the value of filepath is wrong, the file is not found, trying to return an error, and error is not of datatype string.You have

filepath$ = downloadfolder & “\” & object.Source

there. The double backslash looks strange for me, there is no need to use it for LotusScript.

Try adding a msgbox filepath$ before the line with an error, to check if filepath is correct. And replace \ by just single .

Subject: RE: Type mismatch error

If Dir$( filepath$) Then

The return from Dir$ will either be the file path string or “”, neither of which is convertable to a Boolean or Variant containing a Boolean-tagged integer.

Subject: RE: Type mismatch error

I’ve changed the “\” for a "" and added a Msgbox command in 2 places (1 before the If statement and 1 after). The agent does not make it to the second msgbox and still gives the same error msg.

Here’s the up[dated 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 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 ) '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 + "\"+ filen(filecounter))="" Then 	'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

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

				Msgbox "The file path = " + filepath$ + "."

				If Dir$( filepath$) Then

					Msgbox "The file path = " + filepath$ + "."

					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

	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

Subject: RE: Type mismatch error

Hi,

Stan Roger is right (trust his answers, he is always right!):

dir$(…) does not return a boolean value, but a string.

You may use the following statements:

if dir(filepath$) = “” then FileDoesNotExist

if dir(filepath$) <> “” then FileDoesExist

Regards, Natalie

Subject: RE: Type mismatch error

That’s because you didn’t correct the error Stan pointed out to you. The statement:

If Dir$( filepath$) Then

is illegal because if expects a value of True or False, and Dir$ does not return True or False; it returns a string. It’s hard to be sure what the intention was here because there are no comments, but possibly it should be:

If Dir$( filepath$) = “” Then

Now the test should return True if there is no such file – so the code inside the if would create the file if it is not already there.