PDA

View Full Version : Solved: Read smth from tex file. How ?



separator
04-21-2005, 06:25 AM
Hello friends !
So subj:
I try to run scripts from VBA but it's not working:
Sub OpenTextFileTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, TristateFalse)
f.Write "Hello world!"
f.Close
End Sub

However I need to get numbers from Text file.
So try to modify another example from VBA help:

Sub TextStreamTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile "c:\test1.txt" 'Create file
Set f = fs.GetFile("c:\test1.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
s = ts.ReadLine
MsgBox s
ts.Close
End Sub

I think I need not create file but just open it. But can't find procedure for it !

Regards,
separator

Killian
04-21-2005, 06:56 AM
If you have a file already you just need to define it's path and open itSub TextStreamTest()

Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
Dim fs, f, ts, s


Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(strFilePath)
Set ts = f.OpenAsTextStream(1, -2)
s = ts.ReadLine
MsgBox s
ts.Close

End SubThis will read the first line of the text file. If you want to read other lines then just loop through until the line number and return the last oneSub TextStreamTest2()

Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
Dim fs, f, ts, s
Dim i As Long


Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(strFilePath)
Set ts = f.OpenAsTextStream(1, -2)
For i = 1 To InputBox("Enter line number:", "Read from text file")
s = ts.ReadLine
Next
MsgBox s
ts.Close

End Sub

mvidas
04-21-2005, 06:59 AM
Hi separator,

Your examples look more like vbscript examples than in vba (due to the const statements). But nonetheless, I can show you how to do what you need:

Set ts = fs.OpenTextFile("C:\test1.txt", ForReading)
'Do Until ts.AtEndOfStream
s = ts.ReadLine
MsgBox s
'Loop
ts.Close

I've commented out the part that will loop through the entire text file to be read. You could also use ts.ReadAll to input the entire text file.

Why not just use the Open method build into vba/vb? If you're only going into a specific file and not needing to recurse subdirectories (the only really useful point of FSO) then the build in functions would probably be better. They are a little quicker, and you can do the same things:

Open "C:\test1.txt" For Input As #1
'Do Until EOF(1)
Line Input #1, s
MsgBox x
'Loop
Close #1

Please let us know if you have any specific questions about FSO or any of these file-accessing methods!
Matt

separator
04-21-2005, 07:21 AM
Thanx a lot for all!:beerchug:
I try all your examples it's working well!
I just don't know how it work in VBA! New language, just started whith it!:)
Now I know!

Regards!

separator
04-22-2005, 05:10 AM
Hei one more question. How to write smth in file in the same procedure ? How i can understand it's not ok ? :


Sub TextStreamTest()

Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
Dim fs, f, ts, s , m

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(strFilePath)
Set ts = f.OpenAsTextStream(1, -2)
s = ts.ReadLine
MsgBox s
m = "500"

ts.write m

ts.Close

End Sub

Killian
04-22-2005, 08:03 AM
You'll need to close the file (it's still in read mode), them open it to append a line on the end.
Also, don't forget to change the file path from my desktop path to your file's location or it won't find it!!!Sub TextStreamTest()

Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
Dim fs, f, ts, s, m

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(strFilePath)
Set ts = f.OpenAsTextStream(1, -2) 'open mode "For Reading"
s = ts.ReadLine
ts.Close
MsgBox s

m = "500"
Set ts = f.OpenAsTextStream(8, -2) 'open mode "For Appending"
ts.WriteLine m
ts.Close

End SubP.S. I deleted the other duplicate thread for this question

separator
04-22-2005, 08:12 AM
Thanx Killian!
Can also said me how to delete information in file before put smth new in it ?
Also where I can find what else operations I can do with file in VBA? (rename, move, delete, edit, etc.)

Thanx!

Killian
04-22-2005, 08:36 AM
If you open the file for Write instead of Append it will replace all the text in the file with what you write in.
The best thing for you to do is to learn about the FileSystemObject - there's a lot you can do!
Here's a good link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsfsotutor.asp