Hi all,
Quite new to VBA but would be very grateful if someone could help me with the below.
I can't figure out the code I need in order to go into a text file, remove lines 10, 11,12, 13 & 14 in the text file.
I've seen threads with the find and replace in a text file but cant use these as it's not the same.
I want to completely remove a specific line in my text file. Any help would be greatly appreciated.
Code so far:
Dim fso As Object '//FileSystemObject
Dim ts(1) As Object '//TextStream
Dim s As String, t As String
Dim FileContents As String
Dim strFilePath As String
strFilePath = ActiveSheet.Range("C3") & ActiveSheet.Range("C4") & ".xml"
Const SEARCH_FOR1 As String = "Line10" & "Line11" & "Line12" & "Line13" & "Line14" of text file"
Const REPLACE_WITH1 As String = ""
s = strFilePath
If s <> "False" Then
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strFilePath) Then
t = fso.GetParentFolderName(s) & "" & Replace(fso.GetTempName(), ".tmp", ".xml")
Name s As t
Set ts(0) = fspenTextFile(t, 1, False, -2)
FileContents = ts(0).ReadAll
ts(0).Close
Set ts(0) = Nothing
FileContents = Replace(FileContents, SEARCH_FOR1, REPLACE_WITH1)
Set ts(1) = fspenTextFile(s, 2, True, -2)
ts(1).Write (FileContents)
ts(1).Close
Set ts(1) = Nothing
fso.DeleteFile (t)
End If
End If
Quite new to VBA but would be very grateful if someone could help me with the below.
I can't figure out the code I need in order to go into a text file, remove lines 10, 11,12, 13 & 14 in the text file.
I've seen threads with the find and replace in a text file but cant use these as it's not the same.
I want to completely remove a specific line in my text file. Any help would be greatly appreciated.
Code so far:
Dim fso As Object '//FileSystemObject
Dim ts(1) As Object '//TextStream
Dim s As String, t As String
Dim FileContents As String
Dim strFilePath As String
strFilePath = ActiveSheet.Range("C3") & ActiveSheet.Range("C4") & ".xml"
Const SEARCH_FOR1 As String = "Line10" & "Line11" & "Line12" & "Line13" & "Line14" of text file"
Const REPLACE_WITH1 As String = ""
s = strFilePath
If s <> "False" Then
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strFilePath) Then
t = fso.GetParentFolderName(s) & "" & Replace(fso.GetTempName(), ".tmp", ".xml")
Name s As t
Set ts(0) = fspenTextFile(t, 1, False, -2)
FileContents = ts(0).ReadAll
ts(0).Close
Set ts(0) = Nothing
FileContents = Replace(FileContents, SEARCH_FOR1, REPLACE_WITH1)
Set ts(1) = fspenTextFile(s, 2, True, -2)
ts(1).Write (FileContents)
ts(1).Close
Set ts(1) = Nothing
fso.DeleteFile (t)
End If
End If