File Editing and Formatting
Hello all,
I have an application that simply takes a formatted text file and replaces a phone number in it. The user just simply adds the phone number and then click on the command button and it reads the file into memory, replaces the string, and then writes it back to file. It does everything Just great!
My problem is the fact that it cuts ALL my whitespaces and formatting from the file. If this file is off by one whitespace, my decoder will not work at all. My file goes from 9k to 2k because this code cuts out all my white spaces. Anyone have any ideas how I can accomplish my mission while keeping the file format intact? Here is my code:
Option Explicit
Private Sub Command1_Click()
Dim strTextFile As String
Dim strTextLine As String
Dim strPhoneNumber As String
strPhoneNumber = Text1.Text ' Get the phone number
Open "C:\temp\Test.txt" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, strTextLine ' Read line into variable.
strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
Loop
Close #1 ' Close file.
strTextFile = Replace(strTextFile, "<PhoneNumber>", strPhoneNumber) 'adds the public text vatable that the user input overwriting the <PhoneNumber> text
Open "C:\temp\Test.txt" For Output As #1
Print #1, strTextFile
Close #1
End Sub
Timothy H. Schilbach
Alpha Omega Design Inc.
[email protected]
Re: File Editing and Formatting
TRy:
Dim strTextFile as string
Dim strTextLine as string
strTextFile = "Blabla blabla " & vbCrLf & " ababa 2 ababa ababa "
Open "c:\txtmy.txt" for Output as #1
print #1, strTextFile
Close #1
strTextFile = ""
Open "c:\txtmy.txt" for input as #1
Do While Not EOF(1) ' Loop until end of file.
Line input #1, strTextLine ' Read line into variable.
strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
Loop
Close #1
'strip last terminator
strTextFile = Left$(strTextFile, len(strTextFile) - 2)
strTextFile = Replace(strTextFile, "2", "024043", , , vbBinaryCompare)
Open "c:\txtmy.txt" for Output as #1
print #1, strTextFile
Close #1
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
Re: File Editing and Formatting
Thank you for the code. I adapted it to my situation and it stille truncated ALL whitespaces out of my file. Would you be interrested in seeing the before and after result of this code? I can zip them and send them too ya.
Here is the script adaption:
Option Explicit
Private Sub Command1_Click()
Dim strTextFile As String
Dim strTextLine As String
Dim strTextInput As String
strTextInput = Text1.Text
strTextFile = ""
Open "c:\temp\dialup\default.set" For Input As #1
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, strTextLine ' Read line into variable.
strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
Loop
Close #1 'strip last terminator
strTextFile = Left$(strTextFile, Len(strTextFile) - 2)
strTextFile = Replace(strTextFile, "<PhoneNumber>", strTextInput, , , vbBinaryCompare)
Open "c:\temp\dialup\default.set" For Output As #1
Print #1, strTextFile
Close #1
End Sub
Timothy H. Schilbach
Alpha Omega Design Inc.
[email protected]
Re: If there is anything...
...Just let me know if it worked in your case...
Best regards,
Cesare
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.