VBA编辑文本文件的第三行

我目前有一个文本文件(c:\ temp \ temp.txt),我想能够使用VBA来编辑文件,并消除第三行string数据(它是可变的,所以我不知道它是什么会说),但保持其余的文本行保持不变。

我一直在努力弄清楚,似乎我必须打开文件,将整个文件保存为一个string,然后closures并重新打开文件并编辑string并保存?

任何帮助将非常感激!

你的伪代码几乎是你需要做的。 我亲自拆分一个换行符,然后写回个别行:

Private Sub KillLineThree(filepath As String) With CreateObject("Scripting.FileSystemObject") Dim lines() As String With .OpenTextFile(filepath) lines = Split(.ReadAll, vbCrLf) .Close End With Dim i As Long With .CreateTextFile(filepath) For i = LBound(lines) To UBound(lines) If i <> 2 Then .WriteLine lines(i) Next .Close End With End With End Sub 

作为一个稍微矫枉过正的版本,你可以使用Excel

 Workbooks.Open "c:\temp\temp.txt" Rows(3).Delete DisplayAlerts = False ActiveWorkbook.Close True DisplayAlerts = True