Write in a text file

Posted on 2001-08-14
Last Modified: 2012-06-27
How open, append, text files.
and how write in that files.

Question by:juliosb73
  • 3
  • 2
  • 2
  • +5
LVL 20

Accepted Solution

hes earned 50 total points
ID: 6386143
For writing
Open "Your file" for output as #1
Print #1 "your Data"
Close #1
For appending

Open "Your file" for Append as #1
Print #1 "your Data"
Close #1


Expert Comment

ID: 6386294
There were typos in that, let me clarify and demonstrate more of File I/O.

Dim FileHandle As Long
FileHandle = FreeFile ' Get free file handle
Open "Filename" For Output as #FileHandle ' Open file for output
Print #FileHandle, "My String" ' Prints string, adds CR+LF after.
Write #FileHandle, FileHandle, "Hello", 5.5 ' Writes variables and values seperated by commas, adds CR+LF after.
Close #FileHandle

Open "Filename" For Append as #FileHandle ' Open file for output, without deleting current contents first
Print #FileHandle, "New Text Appended"
Close #FileHandle

Open "Filename" For Binary As #FileHandle ' Open file for direct binary access. Allows writing almost any types of data, and at any location in the file.
Put #FileHandle, 1, "Hello, World!" ' Get and Put are used to retrieve variables or write variables/data from specific locations in binary files. You cannot use Write and Print in binary files. Note that the 1 (location) is optional.
Close #FileHandle

Good luck :)

Expert Comment

ID: 6387314
My recommendation and experience told me, if u have Internet Explorer installed or look for this file in you Windows\ directory: scrrun.dll

Include Microsoft Scripting Runtime in project reference.

This is called FileSystemObject, very powerful and fast.

Do like this:

Dim fso as New FileSystemObject
Dim f as TextStream

set f = fso.OpenTextFile("C:\test.txt",False, ForAppending)
f.write("abc") & vbcrlf
set f = nothing

This is simple enough, look for MSDN library for details, it have a lots of useful functions and properties.

Hope this helps you.


Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 6387716

To prevent potential conflicts among the experts, I would like to give you some pointers on being an expert.

        1. You will benefit from carefully reading the entire thread of the question. This will
         prevent you from posting a duplicate suggestion. It also may give you the insight in
         posting *the right answer* that the other experts may have missed. You should not
         repost or copy a previously posted comment as your answer.

        2. Remember, that we are all human. Some people will have a bad day (including you)
         now and then, and may not maintain the most professional of demeanors. That is going
         to happen, so my advice is to always keep in mind that the written medium can be the
         most difficult one in which to express one's thoughts.

        3. If you are not confident that your answer will solve the entire problem without additional
         troubleshooting, then post it as a comment.

        4. If you are confident that your answer will solve the problem without any further troubleshooting,
         post it as an answer.

        5. Some topic areas move very fast and you must post as an answer if you hope to ever get points in
         that thread. The C++ topic area is an example. Experts there will post an answer of 'Answer coming'
         and then post a comment following that with the answer. This is because the Expert needs to actually
         create the data, script etc. Other topic areas move slower, and some of the experts have adopted a
         policy of posting comments only until the customer indicates who should post for the points. You will
         have to feel you way through those areas.

Thank you,
Community Support @ Experts Exchange


Expert Comment

ID: 6388394
No use FSO for this!

LVL 43

Expert Comment

ID: 6388468
costello, should you not have forced the rejection of gigsvoo's proposed answer?
LVL 43

Expert Comment

ID: 6388479
And please do the same on gigsvoo again I am afraid.

Expert Comment

ID: 6388740
Hi gigsvoo,

Askers can accept any comment made to their questions as the answer. Because of that, a lot of experts never answer a question, but post comments only.

I don't think you necessarily need to adhere to that. If an asker asks "what is 1 + 1" I can't blame the first person in when they lock the question with the answer "2". But most questions aren't as clear cut as that.

In the wonderful world of computing, there often are many different ways to "skin a cat" (as the saying goes).

A lot of times you cannot be *absolutely* sure that your suggestion is going to address the question.

Very often, other participants have already made suggestions.

If any of this is true, you should only post your suggestions as a comment.

Like any web community, Experts-Exchange has a netiquette that members adhere to. Some of this is in the form of "unwritten rules", but a lot of it is described in our member agreement. You can find that here:

Please also read our tips on comments and answers:

Thank you for your understanding, and I do expect you to be a bit less eager in the future.

To the asker:
It is in your own interest to reject a proposed answer as soon as possible if it doesn't fully address your problem. As long as a question is "locked" with a proposed answer, a lot of experts will not bother looking at it. Once you've rejected the question, it will become "open" again, which is like an invitation to experts to participate in your question.


Community Support

Expert Comment

ID: 6388756
PS - please note that I have only force-rejected the proposed answer because of the above reasons. juliosb73, if you think that gigsvoo's answer happens to be the best one, you can still accept it.

Expert Comment

ID: 6391056
Sorry for all those hassel, i had found out to use Comments after I had posted 2-3 replies using Answer.

Forgive me coz i m new to here.

Apologize to everyone...


Expert Comment

ID: 6391707
Hi gigsvoo, don't worry about it. We all had to be told the first time we joined, the guidelines are not as clear as they should be :-)

Thank you for taking it as intended. Like I said, Welcome!
LVL 49

Expert Comment

ID: 7199958
Hi juliosb73,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept hes's comment(s) as an answer.

juliosb73, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 7213381
Per recommendation, force-accepted.

CS Moderator

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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