Re: How to use TextStream object

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Mike,

Is that the sort of thig you're looking for?
Yes, thanks so much. The snippet was very lucid.

Ben

--



"Mike Williams" wrote:

"Ben" <Ben@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:16592723-8E77-4E21-A849-C51035E0729D@xxxxxxxxxxxxxxxx

You are absolutely right.

Okay. You haven't said whether you are using VB6 or perhaps VBA in Microsoft
Word or whatever, but either way you should forget all about that
"textstream object" thing. Standard VB I/O is much better, and the following
code should work for you in either case. First, create a text file (just for
test purposes) by typing the following five lines shown between the rows of
asterisks (including the "blank" line) *exactly as shown* into a text editor
(Notepad for example) and saving it to your c:\ drive under the filename
"myfile.txt" . . .

********************************
This is line one, okay.

This is line 3 because of the blank line
Perhaps this is the line you're looking for?
Okay. This is line 5.
********************************

Now open a VB Form and place a Command Button on it. Paste the following
code into the Command Button's Click event:

Dim fn1 As Long, fn2 As Long, s As String
Dim n As Long, nfound As Long
Dim s1 As String, s5 As String
fn1 = FreeFile
Open "c:\myfile.txt" For Input As fn1
fn2 = FreeFile
Open "c:\myresult.txt" For Output As fn2
While Not EOF(fn1)
n = n + 1
Line Input #fn1, s
s1 = Mid$(s, 1, 1)
s5 = Mid$(s, 5, 1)
If s1 = "P" And s5 = "a" Then
nfound = nfound + 1
Print #fn2, s
End If
Wend
Close fn1
Close fn2
Caption = Format(nfound) & " matches found."
Close fn1
Close fn2

Now run the program and click the botton. The code should load and examine
the file "c:\myfile.txt" and it should produce from it a new file
"c:\result.txt". The Form's caption should show that the code found 1 match.
You can then examine both of these txt files in a standad text editor to see
what they contain. Is that the sort of thig you're looking for?

Mike






.


Quantcast