Writing strings to a text file ?

D

Dan Thompson

This is the code I am using to write strings to a .txt file

Function WriteLineNums()
Dim X as Integer
Dim S As String
S = "Line #"
Open "C:\MyTextFile.txt" For Output As #1
For X = 1 To 3
Write #1, S & X
Next X
Close #1
End Function

The problem is I don't want the function to write the Double quotes to the
file.
anyhow this is what the function writes to MyTextFile.txt
"Line #1"
"Line #2"
"Line #3"

And This is what I want it to write
Line #1
Line #2
Line #3

What am I doing wrong ?
Any thoughts would be appreciated.

Thanks
Dan Thompson
 
T

Trevor Shuttleworth

Try using Print instead of write

An example from the VBA Help:

Sub test2()
Open "TESTFILE.TXT" For Output As #1 ' Open file for output.
Print #1, "This is a test" ' Print text to file.
Print #1, ' Print blank line to file.
Print #1, "Zone 1"; Tab; "Zone 2" ' Print in two print zones.
Print #1, "Hello"; " "; "World" ' Separate strings with space.
Print #1, Spc(5); "5 leading spaces " ' Print five leading spaces.
Print #1, Tab(10); "Hello" ' Print word at column 10.
Close #1
End Sub

Regards

Trevor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top