Gcode script

hello,
i ve got a problem with a script,it return a error message about the last loop , in frenchy: Next non attendu:
Option Explicit
'Script written by
'Script copyrighted by
'Script version mercredi 23 août 2017 16:32:10
'generer le Gcode helicoide plateau tournant.

    Call Main()
    Sub Main()
    	Dim arrsections,trisections,strsections,n,i,arrpt,angle,nbpt,p
    	Dim angle1, angle2
    	p = 0
    	n = 1
    	'-------------------------------------------------------------creation fichier
    	Dim fso,f
    	'Create file system object (FSO) to be able to work with file system
    	Set fso = CreateObject("Scripting.FileSystemObject")
    	Const ForReading = 1, ForWriting = 2, ForAppending = 8
    	'definition du chemin du dossier d'enregistrement
    	Dim chemo,chem,nomfichier,nprog,pos,seq,newchem,chemprog,chemprogtemp
    	chemo = Rhino.documentPath
    	If  Not isnull(chemo) Then
    		'definition du nom de programme
    		nprog = rhino.GetString("nom de programme")	
    		If isnull(nprog) Then Exit Sub
    	End If
    	
    	pos = instr(chemo, "ETUDE")
    	If pos = 0 Then pos = instr(chemo, "Etude")
    	
    	seq = Split(chemo, "\")
    	newchem = left(chemo, pos - 1)
    	chemprogtemp = newchem & "USINAGE" 
    	If Not fso.FolderExists(chemprogtemp) Then
    		fso.CreateFolder chemprogtemp
    	End If
    	chemprog = chemprogtemp & "\" & "PROGPSE"
    	If Not fso.FolderExists(chemprog) Then
    		fso.CreateFolder chemprog
    	End If
    	nomfichier = chemprog & "\" & nprog & ".iso"
    	
    	
    	'-------selection de la trajectoire
    	Do
    		strtraj = rhino.GetObject("selectionne la première trajectoire")
    		'-------sortie de la boucle
    		If  isnull(strtraj) Then
    			Exit Do
    		End If
    	
    		'valide le sens de coupe
    		Rhino.SelectObject strtraj
    		Rhino.Command("_Dir")
    		
    		'-determine les angles
    		angle1 = rhino.GetInteger("valeur angle départ")
    		angle2 = rhino.GetInteger("valeur angle arrivée")
    		deltangle = angle2 - angle1
    		arrpt = rhino.curvepoints(strsections)
    		nbpt = ubound(arrpt)
    		dangle = deltangle / (nbpt - 2)
    		
    		'entête de programme----------------------------------------
    		If p = 0 Then
    			Set f = fso.OpenTextFile(nomfichier, ForAppending, True)
    			f.write("//NOME=" & nprog)
    			f.write(chr(13) & chr(10) & "//DIMX=" & 0)
    			f.write(chr(13) & chr(10) & "//DIMY=" & 0)
    			f.write(chr(13) & chr(10) & "//BASEDISCO=" & 0)
    			f.write(chr(13) & chr(10) & "F13" & chr(13) & chr(10) & "HM10" & chr(13) & chr(10) & "G92" & chr(13) & chr(10) & "G60" & chr(13) & chr(10) & "G63" & chr(13) & chr(10) & "G99")
    			p = 1
    		End If
    		
    		f.write(chr(13) & chr(10) & "G62" & chr(13) & chr(10) & "$PASSATA=" & n)
    		n = n + 1
    		
    		'--------------définition origine section
    		
    		If arrpt(0)(0) > arrpt(nbpt)(0) Then
    			
    			origine(0) = arrpt(nbpt)(0)
    			origine(1) = arrpt(nbpt)(1)
    		Else
    			origine(0) = arrpt(0)(0)
    			origine(1) = arrpt(0)(1)
    		End If
    		
    		
    		'-------ecriture des lignes
    		
    		For i=0 To nbpt
    			
    			If i = 0 Then
    				vx = round(arrpt(i)(0) - origine(0), 3)
    				vy = round(arrpt(i)(1) - origine(1), 3)
    				f.write(chr(13) & chr(10) & "G01" & " X" & vx & " Y" & vy & " Z" & vx & " A" & vy & "B" & angle1)
    				angle = angle1
    			Else If i = ubound(arrpt) Then
    				angle = angle - dangle
    				vx = round(arrpt(i)(0) - origine(0), 3)
    				vy = round(arrpt(i)(1) - origine(1), 3)
    				f.write(chr(13) & chr(10) & "G01" & " X" & vx & " Y" & vy & " Z" & vx & " A" & vy & "B" & angle)
    				angle = angle + dangle
    			Else
    				vx = round(arrpt(i)(0) - origine(0), 3)
    				vy = round(arrpt(i)(1) - origine(1), 3)
    				f.write(chr(13) & chr(10) & "G01" & " X" & vx & " Y" & vy & " Z" & vx & " A" & vy & "B" & angle)
    				angle = angle + dangle
    			End If	
    		Next
    		
    		
    	Loop
    End Sub