If you're going to write it with ADODB.Stream then perhaps you should read it using the same. See below:
'<---CONTACTS BACKUP--->
Private Sub CommandButton1_Click()
Dim rng As Range, lRow As Long
Dim stOutput As String, stNextLine As String, stSeparator As String
Dim stFilename As String, stEncoding As String
Dim fso As Object
Set rng = Worksheets("Contacts").UsedRange
stFilename = "C:\Users\" & Environ("Username") & "\Desktop\BackUp.txt"
stSeparator = vbTab
stEncoding = "UTF-8"
For lRow = 1 To rng.Rows.Count
If rng.Columns.Count = 1 Then
stNextLine = rng.Rows(lRow).Value
Else
stNextLine = Join$(Application.Transpose(Application.Transpose(rng.Rows(lRow).Value)), stSeparator)
End If
If stOutput = "" Then
stOutput = stNextLine
Else
stOutput = stOutput & vbCrLf & stNextLine
End If
Next lRow
Set fso = CreateObject("ADODB.Stream")
With fso
.Type = 2
.Charset = stEncoding
.Open
.WriteText stOutput
.SaveToFile stFilename, 2
.Close
End With
Set fso = Nothing
MsgBox "Contact list saved!"
Unload Me
End Sub
'<---CONTACTS RESTORE--->
Private Sub CommandButton2_Click()
Dim MyInputFile As String
Dim FileData As String
Dim LineData As Variant
Dim LineCols As Variant
Dim fso As Object
Dim lRow As Long
MyInputFile = "C:\Users\" & Environ("Username") & "\Desktop\BackUp.txt"
Set fso = CreateObject("ADODB.Stream")
With fso
.Type = adTypeText
.Charset = "UTF-8"
.Open
.LoadFromFile MyInputFile
FileData = .ReadText
.Close
End With
Set fso = Nothing
LineData = Split(FileData, vbCrLf)
For lRow = 0 To UBound(LineData)
LineCols = Split(LineData(lRow), vbTab)
Worksheets("Contacts").Range("A" & lRow + 1).Resize(1, UBound(LineCols) + 1).Value = LineCols
Next lRow
MsgBox "Contact list restored!"
Unload Me
End Sub
Note I made a small change to the Backup routine to close the stream. I mostly re-wrote the restore routine.
WBD
Bookmarks