Public Function GetFilePart(ByVal sPath As String, ByVal lLineStart As Long, ByVal lLineStop As Long) As String
'sPath -> chemin, fichier DOIT exister
'lLineStart -> ligne de départ, commence à 1
'lLineStop -> ligne de fin, peut être supérieur au nombre total
'lLineStart et lLineStop doivent être logiques, pas de la ligne 8 à 5 par exemple
Dim FF As Integer
Dim asLines() As String
Dim sLine As String
Dim i As Long
FF = FreeFile
i = 0
ReDim asLines(lLineStart To lLineStop)
Open sPath For Input As #FF
Do While Not EOF(FF)
i = i + 1
Line Input #1, sLine
If (i >= lLineStart) And (i <= lLineStop) Then
' numéro ligne en cours est dans la plage, on conserve
asLines(i) = sLine
ElseIf i > lLineStop Then
' dernière lignes "voulue" lue, pas besoin de parcourir le reste
Exit Do
End If
Loop
Close #FF
' dépassement?
If lLineStop > i Then ReDim Preserve asLines(lLineStart To i)
' retour
GetFilePart = Join(asLines, vbCrLf)
Erase asLines
End Function
'
' ---------------------
' EXEMPLE D'UTILISATION
' on considère un fichier contenant 20 lignes
' ---------------------
Private Sub Exemple()
' retourne tout
MsgBox GetFilePart("c:\mon_fichier.txt", 1, 66)
' retourne les 5 dernières lignes
MsgBox GetFilePart("c:\mon_fichier.txt", 15, 20)
MsgBox GetFilePart("c:\mon_fichier.txt", 15, 300)
' retourne 5 lignes, depuis la l0ème
MsgBox GetFilePart("c:\mon_fichier.txt", 10, 15)
End Sub