Open Notepad and Find/Replace (1 Viewer)

Eugene-LS

Registered User.
Local time
Tomorrow, 01:49
Joined
Dec 7, 2018
Messages
354
there's a lot of Russian comments
Although... fu...k it all!
Whoever wants it will understand - or ask me.
...
See to "mod00Main" module - Private Sub TestTXTfile() ...

Forgive my rudeness - I studied English in the Pubs of the good port city of Hull (Kingston upon Hull)
 

Attachments

  • DatabaseCT_02.zip
    24.6 KB · Views: 21
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 15:49
Joined
Oct 29, 2018
Messages
19,173
The problem is that if I try to edit the text file using VBA it see this character and thinks it's at EOF and exits.
Perhaps if you could tell us what kind of edit you are trying to do with the text file using VBA, we might be able to give you a better solution. If you just need to read the text file into a variable, then maybe you can take a look at this blog article.

 

D_Walla

New member
Local time
Today, 23:49
Joined
Aug 1, 2021
Messages
27
The following seems to strip the offending characters from the file. Does this work for you? You should only need to update the file path in the principal subroutine, CleanTextFile.
Code:
Sub CleanTextFile()

    Dim Txt         As String
    Dim Filename    As String
    Dim NewFilename As String
   
    Filename = "C:\PATHTOFILE\Test.txt"
    Txt = ReadFile(Filename)
    Txt = Replace(Replace(Txt, Chr(vbNull), ""), Chr(26), "")
    NewFilename = Replace(Filename, ".txt", "_FIXED.txt")
    CreateFile NewFilename, Txt

End Sub

Sub CreateFile(ByVal Filename As String, ByVal Contents As String)
   
    Dim FileHandled As Long
    FileHandled = FreeFile
   
    Open Filename For Output As #FileHandled
        Print #FileHandled, Contents
    Close #FileHandled
End Sub
Function ReadFile(ByVal Filename As String) As String
   
    Dim EOFile      As Long
    Dim FileHandled As Long
    Dim Contents    As String
   
    FileHandled = FreeFile
   
    Open Filename For Binary As #FileHandled
        EOFile = LOF(FileHandled)
        Contents = Space$(EOFile)
        Get #FileHandled, , Contents
    Close #FileHandled
   
    ReadFile = Contents
End Function
 

KitaYama

Active member
Local time
Tomorrow, 07:49
Joined
Jan 6, 2022
Messages
547
I'm waiting for your approval
I think you have misunderstood me with the OP.
I have no problem. The file or the question is not mine. So my approval is meaningless.
I just wanted to see how you guys solve OP's problem.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:49
Joined
May 7, 2009
Messages
16,826
and another demo to remove unwanted characters.
 

Attachments

  • CleanAndOpenTextFile.accdb
    408 KB · Views: 24

Mike Krailo

Active member
Local time
Today, 18:49
Joined
Mar 28, 2020
Messages
634
Thanks for that Arnel, that took what I was doing to the next level and whether or not the OP needs it, I certainly find your demo very useful as it can strip all the control characters out of the file from anywhere. Allowed control characters are BS, Line Feed, and Carriage Return.

Edit: Darn, I see now that the replacement character cannot be vbNullString which is unfortunate since I'm guessing that is more in line with what the OP was trying to do. I put an extra step in Arnel's routine to handle that by first converting those unwanted characters to a unique character (a space won't work), then doing a standard replace on it. Now it functions the way I would expect it to.
 

Attachments

  • CleanAndOpenTextFile_v2.zip
    58.8 KB · Views: 24
Last edited:

Users who are viewing this thread

Top Bottom