• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Write in a text file

How open, append, text files.
and how write in that files.

  • 3
  • 2
  • 2
  • +5
1 Solution
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

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 :)
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.


Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.


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

No use FSO for this!

TimCotteeHead of Software ServicesCommented:
costello, should you not have forced the rejection of gigsvoo's proposed answer?
TimCotteeHead of Software ServicesCommented:
And please do the same on http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20168512 gigsvoo again I am afraid.
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
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.
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...

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!
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
Per recommendation, force-accepted.

CS Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now