Ok... I believe I am getting somewhere. I scoured the net and found the
following code. I created a sample form with 3 text boxes (Text1,Text2,Text3)
I created 2 buttons(replace text and replacefile)
Replacetext give me and error 2185 focus error
ReplaceFile does indeed creat the replacement file - but nothing is in it.
Can someone Please take a look at the code and give me a clue as to what may
be going on. I am really trying to learn and am not asking to have code
rewritten.
Thanks, Again
Jack
Code Follows:
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
Jack said:
Tim,
Thank you for the reply. These are TRULY fixed targets. Generally the
##replace1## field within the txt file needs to replaced with one - three
words MAX. If this was a onesy twosey (did i spell that right

)I wouldnt
have a problem. But 100+ pages need to be updated with every file having
different data. I created a template, went through and made the targets
##replace1##, ##replace2## and ##replace3##. I then saved this as a text
file.
During my endless attempts to figure out how to do this. I have taken the
following steps. I just need assistance putting it together.
I created a table named tbl_replace - 3 text fields named replace1,2,etc I
added a fourth field as to what I will need the output filename to be.
I created a form with a command button and inserted the following code.
Sub ReplaceFile(Tempfile As String, ReplaceFile As String, Offset As Integer)
On Error GoTo Err_ReplaceFile
Dim db As DAO.Database
Dim Directory As String
Dim MyString As String
Dim NewString As String
Dim MyChar As String
Dim TagFound As Boolean
Dim i As Integer
Set db = CurrentDb
Directory = Mid(db.Name, 1, Len(db.Name) - Len(Dir(db.Name)))
Open Directory & "\" & Tempfile For Input As #1 !This works fine
Open Directory & "\" & ReplaceFile For Output As #2! I havent figured out
how to get access to loop through all of my records. Maybe a d lookup?
TagFound = False
'loop through text file
Do While Not EOF(1)
NewString = ""
'read one line
Line Input #1, MyString
Do Until instr(MyString) = 0
'walk down the line
!This is part I am confused about
something like:
replace(mystring,##replace1##,[field from tbl_replace])
replace(mystring,##replace2##,[field from tbl_replace])
replace(mystring,##replace3##,[field from tbl_replace])
Loop
'Output the line Record
Print #2, NewString ! is this best way to do this?
Loop
MsgBox "Done"
Exit_ReplaceFile:
Close #2
Close #1
Set db = Nothing
Exit Sub
Err_ReplaceFile:
If Err.Number = 62 Then
Resume Exit_ReplaceFile
Else
MsgBox Err.Number
MsgBox Err.Description
Resume Exit_ReplaceFile
End If
End Sub
Any assistance would be greatly appreciated.
Thanks again,
Jack
Tim Ferguson said:
I don't think it is very similar: the OP, as I remember it, had incredibly
tight requirements that made it easy to offer a simple, but hopelessly
importable, solution.
My first reaction to any kind of generic text manipulation, especially when
it comes to html tags and the like, is to get the PERL book out. If you
really want to use VB and its cousins, the built in Scripting.Regex is
nearly as flexible, but be prepared for a steep learning curve.
If you have really fixed targets, you can use Instr() and Replace() to find
them. The other problem with html type text is that you cannot expect to
find nice line-endings in appropriate places, and you have to work very
hard to make sure you don't miss having more than one target in the same
line.
Best of luck
Tim F