?
Solved

Writing to text files

Posted on 2004-09-28
4
Medium Priority
?
8,228 Views
Last Modified: 2012-08-14
Hello, Experts.  The code bellow writes 2 line to a text file.

open ("c:\document.txt") for output as #1
print #1, "A line of text"
print #1, "Another line of text"
close #1

How can I make the next time i add text to the file for the text to start on the third (first empty row) line?

Can somebody point me to a good VB/VBA text editing site, i need to write and read plain text and CSV text files.

Thanks
0
Comment
Question by:sublimation
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
bingie earned 1200 total points
ID: 12168918
open ("c:\document.txt") for append as #1
print #1, "A line of text"
print #1, "Another line of text"
close #1

Change "output" to "append" and it will append the file next time, otherwise it will over write any data in the file.
0
 
LVL 13

Assisted Solution

by:imarshad
imarshad earned 800 total points
ID: 12168992
Also you may like to have a look at a similar question that I asked.......I got a good Class Library code from sudhakar_koundinya....It is an absolutely brilliant Library for different functions for Text Files......
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21100341.html

Imran Arshad
0
 
LVL 13

Expert Comment

by:imarshad
ID: 12169036
You should add a Class Module in your code and then copy the whole code over there.......

Then you can use the following code......

Private Sub Command1_Click()
Dim o As New TextFile
Dim str As String
o.TextFilePath = "c:\a.txt"
o.WriteNewLine "A line of text"
o.WriteNewLine "Another line of text"
End Sub


with many other features as well........

Imran Arshad
0
 
LVL 9

Expert Comment

by:experts1
ID: 12437129
Try this modification to your code.

This method uses a subroutine to add(append) new text
to the file "document.txt".

Note:
The file is opened for "Append" in subroutine.
     
open ("c:\document.txt") for output as #1
print #1, "A line of text"
print #1, "Another line of text"
close #1

Call AddLine("Some new text")

Private Sub AddLine(ByVal newtext As String)
Open ("c:\document.txt") For Append As #1
  Print #1, newtext
Close #1
End Sub

The above method uses a subroutine to append new text
to the file "document.txt".

Have Fun!!
Experts1
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

762 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