Solved

open text file - read line from text file

Posted on 2001-06-17
14
5,999 Views
Last Modified: 2008-02-26
By using this simple code I am able to write a line to a text file.

Dim fs, otf
Set fs = CreateObject("Scripting.FileSystemObject")
Set otf = fs.OpenTextFile("c:\test.txt", 8, 0)
otf.writeline ("test")
otf.writeline ("test2")
otf.Close


However, in examples that I see in MSDN and elsewhere, objects are inititalized by using the following

Dim fso As New FileSystemObject, txtfile, _
fil1 As File, ts As TextStream

However I get the error message "user defined type not defined."  How come its not defined?  What am I doing wrong.  With the inelli type for diming variables I will see dim x as "integer" but I do not see FileSystemObject, File, or TextStream.

Please show me how to initialize properly so I can at least read on line from a text file.

Thanks
   
0
Comment
Question by:dmontgom
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6199990
Project -> References -> Tick "Microsoft Scripting Runtime". Then follow the MSDN code...
0
 
LVL 33

Accepted Solution

by:
hongjun earned 100 total points
ID: 6199991
Your got to add "Microsoft Scripting Runtime" using Project->References.

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6199996
Oops. Just 1 minute difference. Adding the reference will allow beginners like you to be able to see the relevant methods, events, etc. When you become more and more used to it, then you might consider using the CreateObject method so that only the necessary references/libraries will be included when needed and released when not needed.

hongjun
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6200093
>When you become more and more used to it, then you might consider using the CreateObject method so that only the necessary references/libraries will be included when needed and released when not needed.

Hmmm... I'd always recommend using references so things are strictly typed and early bound. The inclusion of libraries comment is a red herring.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6200105
That's the point. Learn it with including references first so that it will be more understandable.

hongjun
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6200138
What I was saying is "Learn it with including references first" and keep using it through project references, because that's the best way to do it.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6200147
Ya perhaps this is what I am trying to say. Use through project references first and then simply convert those declarations and instantiations.

' Using including Project->References
Dim fso As FileSystemObject
Set fso = New FileSystemObject
...

' Using runtime
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystem")
...

hongjun
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Expert Comment

by:andyclap
ID: 6200160
Aha, we're going around in circles here!
What I'm saying is don't bother using as Object, as early bound explicit references are better if you can use them :)
0
 

Expert Comment

by:crazyburns
ID: 6200528
Are you just trying to read a line from a text file?

use
Open "filename.txt" for INPUT as #1
Input #1 variable1, variable2, ...
Close #1

and to write
Open "filename.txt" for OUTPUT as #1
Write #1 "text", "text2", ...
Close #1

The other way sounds likes it a little more complicated then necessary.
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6201198
There was one question last week in on a missing component for FileScriptingObject used for deleting a folder.

http://www.experts-exchange.com/jsp/qShow.jsp?qid=20134210

The final answer ended at using direct VB file functions like Kill and RmDir. Since, here you don't need to add a reference etc. And no other problems.

So, you could learn FSO for your purposes, but could also use VB directory functions.

But FSO, is of course easier to use.

Cheers.
0
 
LVL 1

Expert Comment

by:CJHarrap
ID: 6201356
Always use early binding.... you get less problems, and the code is more efficent with less overhead.

Cheers,

CJ.
0
 

Author Comment

by:dmontgom
ID: 6202399
Thanks for all of the advice and quick response.

0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6202778
um might I ask why you accepted hongjun's answer if mine is the same and was posted earlier? not that it really matters, but I was just wondering
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6203052
Thats what I also wonder. And, after that, there were some discussions about it. :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now