Private Sub InsertLineInFile(ByVal FileName As String, _
ByVal PositionInsertion As Long, _
ByVal NewValue As String)
Dim f As Integer, errCode As Integer, errString As String
Dim buffer As String
Dim t() As String
Dim i As Long
Dim insertion_done As Boolean
buffer = ReadFileToBuffer(FileName, errCode, errString)
t() = Split(buffer, vbCrLf)
f = FreeFile
Open FileName For Output As #f
For i = 0 To UBound(t()) - 1
If (i >= PositionInsertion) And Not (insertion_done) Then
Print #f, NewValue
insertion_done = True
End If
Print #f, t(i)
Next i
' Insertion à la fin si pas encore fait
If Not (insertion_done) Then
Print #f, NewValue
End If
Close #f
End Sub
' Exemple d'appel
Private Sub Command5_Click()
' Insertion de "JEAN-MARC" en tête de fichier
Call InsertLineInFile("c:\publicdata\test.txt", 0, "Jean-Marc")
j\'ai trouver un code me permettant d\'inserer une phrase à la première ligne d\'un fichier existant.