Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write to a text file

Status
Not open for further replies.

GSMike

Programmer
Feb 7, 2001
143
US
I'm confused...
A few days ago, I got this code (or something like it) from the on-line help files:

Dim fs
set fs=createobject("scripting.filesystemobject")
fs.write("I put my text here")
etc, etc...

Now all I can find is:

Open "c:\testfile.txt" for output as #1, etc.
Print #1, "I can also put my text here..."

What I am really looking for is the former method (i.e., set fs=createobject("scripting.filesystemobject"), etc. I can't find it anywhere in the help files, and I know I've referred to it at least three times in the past there. Can you help by helping me finish the syntax for:

Dim fs
set fs=createobject("Scripting.FileSystemObject")
????something about a text file here
???fs.write(&quot;My text here...&quot;) <--I think?

or by suggesting a keyword for me to type in the contents/index of the Access 97 help files to get back to where I was before?

The former method was more flexible, allowing me to append text to an existing file without starting a new line, etc.

Thanks!!!

:)
 
Have you checked that you still have a reference to the Microsoft Sriptlet Library?
 
Hi!
You're using late binding. Try early binding - Tools | References and add &quot;Microsoft Scripting Runtime&quot;. This will run faster.

Then you can use the following syntax (False:= the file is not created if it's not there);

Sub test()
Dim fs As FileSystemObject
Dim txtFile As TextStream
' IoMode kan be the following constants:
' ForAppending
' ForWriting
' ForReading
Set fs = New FileSystemObject
Set txtFile = fs.OpenTextFile(&quot;C:\test.txt&quot;, ForWriting, False)
txtFile.WriteLine (&quot;Just testing&quot;)
txtFile.Close
End Sub

Roy-Vidar
 
This worked a treat for me as I was previously writing to MS Word and then saving the files as .txt (please forgive me I'm brand new to this).

Only one question: Is there a way to insert the text without the carriage return at the end of the string?

This would help me greatly as I've still got to open the txt file manually to correct this.

Much Appreciated.
 
For appending data with no carriage return, this worked for me...good luck...power to the newbies (I should say, &quot;US newbies)!!

Code:
Sub mike()
Dim fs As FileSystemObject
Dim txtFile As TextStream
Set fs = New FileSystemObject
'Set txtFile = fs.CreateTextFile(&quot;c:\testfile.txt&quot;)
Set txtFile = fs.OpenTextFile(&quot;c:\testfile.txt&quot;, ForAppending)
txtFile.Write (&quot;just testing&quot;)
txtFile.Close
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top