Reputation: 193
I currently have a text file (c:\temp\temp.txt) and I want to be able to use VBA to edit the file and and wipe out the 3rd row of string data (it's variable so I don't know what it will say) but keep the rest of the rows of text intact.
I've been trying to figure it out, it seems like I have to open the file, save the entire file as a string, then close and reopen the file and edit the string and save?
Any help would be much appreciated!
Upvotes: 0
Views: 2068
Reputation: 22876
as a shorter a bit overkill version you can use Excel
Workbooks.Open "c:\temp\temp.txt"
Rows(3).Delete
DisplayAlerts = False
ActiveWorkbook.Close True
DisplayAlerts = True
Upvotes: 1
Reputation: 22205
Your pseudocode is pretty much what you need to do. I'd personally split on a newline and write individual lines back:
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
Upvotes: 4