VBA:读取和写入文本文件的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44545298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:44:02  来源:igfitidea点击:

VBA: Read and Write lines to text file

vba

提问by Quint

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

回答by ASH

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.

'启动程序和子程序编写VBA代码将数据写入文本文件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