Option Explicit
Sub Main
   Dim nEstrInizio,nEstrFine
   Dim DataIni,DataFin
   Dim sFile
   Dim sChrSep
   Dim sDirDest
 
   MsgBox "Sostituisci nello script la directory dove verrà creato il file csv" & vbCrLf & "Poi elimina questo messaggio",vbInformation
   sDirDest = "C:\Users\TuoUser\Desktop\Lotto_Open\Archivio" ' percorso di scarico archivio
   'sDirDest = GetDirectoryAppData
 
   If DirectoryEsistente(sDirDest) Then
       sChrSep = ";"
       sFile = AddSlash(sDirDest) & "EstrazioniLotto.csv"
       If EliminaFile(sFile) Then
       nEstrInizio = 1
       nEstrFine = EstrazioniArchivio
       Call EsportaEstr(nEstrInizio,nEstrFine,sFile,sChrSep)
       End If
   Else
      MsgBox "Directory non trovata" & vbCrLf & sDirDest
   End If
End Sub
Sub EsportaEstr(nEstrInizio,nEstrFine,sFile,sChrSep)
   Dim k,f,idEstr
   Dim sRecord
   Dim nProgr
   Dim sDataCorr
   Dim r,e
   For k = nEstrInizio To nEstrFine
      Call Messaggio("Estrazione : " & k)
      idEstr = k
      ReDim aNum(0)
      If GetEstrazioneCompleta(idEstr,aNum) Then
         nProgr = nProgr + 1
         sDataCorr = Replace(DataEstrazione(idEstr),".","/")
         sRecord = FormatSpace(nProgr,9,True) & sChrSep
         sRecord = sRecord & IndiceAnnuale(idEstr) & sChrSep
         sRecord = sRecord & sDataCorr & sChrSep
       
         For r = 1 To 11
            For e = 1 To 5
               sRecord = sRecord & Format2(aNum(r,e)) & sChrSep
            Next
         Next
         sRecord = RimuoviLastChr(sRecord,sChrSep)
         Call ScriviFile(sFile,sRecord,False,True)
      Else
         Exit For
      End If
      If ScriptInterrotto Then Exit For
      Call AvanzamentoElab(nEstrInizio,nEstrFine,k)
   Next
   Call CloseFileHandle(sFile)
  ' Call LanciaFile(sFile)
  Scrivi "Esportato file " & sFile
  MsgBox "Fine",vbInformation
End Sub
Function AddSlash(s)
   If Right(s,1) <> "\" Then
      AddSlash = s & "\"
   Else
      AddSlash = s
   End If
End Function