Option Explicit
'Constantes d'ouverture fichier textes
Public Const ForReading = 1 'Lecture
Public Const ForWriting = 0 'Ecriture
Public Const ForAppending = 8 'Ajout
'
'Constante de création si fichier inexistant
Public Const Create As Boolean = True
'================================================
' SAUVEGARDER UNE LIST VIEW DANS UN FICHIER CSV
'================================================
Public Sub lvSaveToExcelFile(ByVal LV As ListView, Optional ByRef FileName As String = "", Optional Sep As String = ";")
Dim i As Long
Dim j As Long
On Error Resume Next
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim TXTstream
If FileName = "" Then FileName = ThisWorkbook.Path & "\TSTCSV.Csv"
Set TXTstream = FSO.OpenTextFile(FileName, ForAppending, Create)
With TXTstream
For i = 1 To LV.ColumnHeaders.Count - 1
.Write CStr(LV.ColumnHeaders(i).Text) & Sep
Next
.WriteLine CStr(LV.ColumnHeaders(LV.ColumnHeaders.Count).Text)
For i = 1 To LV.ListItems.Count - 1
.Write LV.ListItems(i).Text & Sep
For j = 2 To LV.ColumnHeaders.Count - 1
.Write LV.ListItems(i).SubItems(j - 1) & Sep
Next
.WriteLine LV.ListItems(i).SubItems(LV.ColumnHeaders.Count - 1)
Next
.Close
End With
Set TXTstream = Nothing
Set FSO = Nothing
End Sub