0

I have managed to hack together a function that reads or writes to lines in a text file. I'm using it to save some settings for my application. Just wanting see everyones input and to see if there is a better way to edit a line in the text file. Specifically the part that builds an array to edit lines. This is what I have.

Public ReadOptionsOutput As String
Public FSO As New FileSystemObject
Public TxtFileOutPut As String

Public Function TxtFile(WritetoFile As Boolean, FileDir As String, line As 
Variant, What As String)
Dim FileContent() As Variant
Dim Idx As Integer
Dim Txtstream As Object

'////////////////Write to file///////////////
If WritetoFile = True Then
Set Txtstream = FSO.OpenTextFile(FileDir, ForReading, False)
Idx = 0

On Error GoTo Err1 'To catch the last blank line. Dont see another way to 
see if .ReadLine is blank
Do 'Build an array to edit lines in Text file
    Idx = Idx + 1
    ReDim Preserve FileContent(1 To Idx)
    FileContent(Idx) = Txtstream.ReadLine
Loop
Err1:
Open FileDir For Output As #1: Close #1 'Delet all text inside of File
Set Txtstream = Nothing
Set Txtstream = FSO.OpenTextFile(FileDir, ForAppending, False)
FileContent(line) = What 'Edit line in the array
    For Idx = 1 To Idx - 1
        Txtstream.WriteLine (FileContent(Idx)) 'Write everything back to 
textfile
    Next

'/////////////////////Read file///////////////
ElseIf WritetoFile = False Then 'Reads Line in file only
Set Txtstream = FSO.OpenTextFile(FileDir, ForReading, False)
NextLine = 1

Do 'Loop thru to selected line and read it
    TxtLine = Txtstream.ReadLine
    If NextLine = line Then
        TxtFileOutPut = TxtLine
    End If
    NextLine = NextLine + 1
Loop Until NextLine > line
End If
Txtstream.Close
End Function
Quint
  • 530
  • 2
  • 8
  • 23
  • You may want to check out SE code review: https://codereview.stackexchange.com/ Stack overflow isn't really supposed to be used for reviewing code. – SaggingRufus Jun 14 '17 at 12:51
  • There is no need to use the FSO at all, there are built in functions that make it easy to load a file into an array with a few lines of code E.g. https://stackoverflow.com/a/11542133/246342 – Alex K. Jun 14 '17 at 13:06
  • you can save settings in the registry with SaveSetting and GetSetting https://msdn.microsoft.com/VBA/Language-Reference-VBA/articles/getsetting-function – Slai Jun 20 '17 at 17:21

1 Answers1

1

I would append data to a text file, from the top down, but not overwriting data that already exists in the text file.

'Starting the program and sub procedure to write VBA code to write the data to a text file Append.

Sub VBA_to_append_existing_text_file()

'Declaring the strFile_Path variable as String Data Type to store the text file path.
Dim strFile_Path As String

'Assigning the Existing File path to the variable strFile_Path.
strFile_Path = "C:\your_path_here\test.txt"

'Opening the text file for Append with FileNumber as 1.
Open strFile_Path For Append As #1

'Writing to the sample text to the File using FileNumber and Write Command.
YourValue = Worksheets("Sheet1").Range("A1").Value
Write #1, YourValue

'Closing the File using FileNumber.
Close #1

End Sub
ASH
  • 20,759
  • 19
  • 87
  • 200