View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Michel Pierron[_2_] Michel Pierron[_2_] is offline
external usenet poster
 
Posts: 63
Default Editing text file

Hi Foss,
If that can help you:

Sub UpDateTxtFile()
Dim FSO As Object, File As Object, Contents As String
Set FSO = CreateObject("Scripting.FileSystemObject")

' Create file to test example:
Set File = FSO.CreateTextFile("c:\examplefile.txt")
Contents = "A quick example of the ReadAll method !!!"
Contents = Contents & vbCrLf
Contents = Contents & "New line for this example !!!"
File.Write Contents
File.Close

' Replacement routine
Const TxtFullPath As String = "c:\examplefile.txt"
Dim ReadFile As Object
Set ReadFile = FSO.OpenTextFile(TxtFullPath, 1, False)
Contents = ReadFile.ReadAll
ReadFile.Close: Set ReadFile = Nothing
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = False
.Pattern = "[!" & vbCrLf & "]"
Contents = .Replace(Contents, "")
End With
Set File = FSO.CreateTextFile(TxtFullPath)
File.Write Contents
File.Close
Set File = Nothing: Set FSO = Nothing
End Sub

Regards,
MP

"Foss" a écrit dans le message de
...
Mornin' all,

I need to open a text file, then replace all occurences of
certain symbols with either nothing or a line return.

I really don't know where to start on this one, has anyone
got some pointers or code that would help me?

Thanks very much,
Foss