Replace text in textbox or file

This is a simple text replacement example. It will also work on Binary files since I included the description on how to make it reusable from a form with 3 text boxes and how to read in a full binary or ascii file as a single string. Usage:

Private Sub cmdReplaceText_Click()
    'replaces all occurances of Text2 in Text1 with Text3
    Call ReplaceTextInTextBox(Text1, Text2, Text3)
End Sub
Private Sub cmdReplaceFile_Click()
    'replaces all occurances of "hello" with "goodbye" in c:\test.txt, and saves the new file as c:\test_replace.txt
    Call ReplaceInFile("c:\test.txt", "c:\test_replace.txt", "hello","goodbye")
End Sub

Place the code below in a module

Public Function ReplaceTextInTextBox(MyTextBox As Object,_
TextOld As Object, TextNew As Object) 'The object reference is made form the form 'you use to call this module. 'Assumed you have a text1, text2 and text3 text box on your form 'where 1 is used for the text you wish to replace text in '2 is the one containing text you are searching for '3 is the new text you wish to see entered in its place. 'The replacement function being called Call ReplaceText(MyTextBox.Text, TextOld.Text, TextNew.Text) 'Replace textview and refresh your form MyTextBox.Text = ReturnValue MyTextBox.Refresh End Function Public Function ReplaceInFile(InputFile As String, _ OutputFile As String, OldT As String, NewT As String) As String Dim Fnum As Integer ' get a FreeFile number Dim FileLength As Long 'Just in case its really big Dim TheString As String
'I open the file as binary to avoid some 'complications with reading in a full file. 'This just so I can skip some trouble shooting with 'some special characters in certain exe files. 'It might be that character you wish to replace. Fnum = FreeFile Open InputFile For Binary As #Fnum ' Open file. FileLength = LOF(Fnum) ' Get length of file. TheString = Input(FileLength, #Fnum) Close #Fnum ' Close file. 'OldT = Whatever text you send form a form or function to search for 'NewT = Whatever text you wish to replace OldT with 'Call the function to replace text Call ReplaceText(TheString, OldT, NewT) 'Now print the result to a new file 'so you dont overwrite your original Fnum = FreeFile Open OutputFile For Output As #Fnum ' Open file. Print #Fnum, ReturnValue ' the ReturnValue from the replacement Close #Fnum ' Close file. End Function Public Function ReplaceText(CleanThis As String, _ OldText As String, _ NewText As String) 'To get the len of the string Dim StrLn As Long 'To split the string Dim PartA As String, PartB As String 'If search string is found, get its start position Dim FoundP As Long Dim OldLn As Long On Error GoTo ErrHandle 'Set a value for the len of the old text to be replaced OldLn = Len(OldText) StrLn = Len(CleanThis) 'Loop through the string until all occurences are eliminated Do While InStr(1, CleanThis, OldText) <> 0 FoundP = InStr(1, CleanThis, OldText) 'Get PartA of the string (before found occurance) PartA = Left(CleanThis, FoundP - 1) 'Get PartB of the string (after found occurance) PartB = Right(CleanThis, StrLn - FoundP - OldLn + 1) '*NOTE ON THE ADDITION AND SUBTRACTION** '****************************************************** '+ 1 to avoid a skip in adding found len and old len 'The previous line could also be written ' 'PartB = Right(CleanThis, StrLn - (FoundP + OldLn - 1)) '****************************************************** 'REBUILD THE STRING BEFORE NEXT LOOP 'This adds a space before and after NewText 'and trims out unneccessary spaces too! CleanThis = Trim(PartA) & " " & _ Trim(NewText) & " " & _ Trim(PartB) 'GET NEW LEN BEFORE LOOPING StrLn = Len(CleanThis) Loop 'Set result of the function ReplaceInString = CleanThis 'Avoid error handling Exit Function 'Add your own error code hereafter ErrHandle: Select Case Err.Number Case Err.Number MsgBox "Your function executed with an error " & Err.Number _
& vbCrLf & vbCrLf & Err.Description, vbExclamation, _
"Error " & Err.Number Err.Clear End Select Resume Next End Function

You might also like...

Comments

Mike J

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic