Text file open and close VBA

K

Karpgam

Hi

I am new to VBA development, I have text file and opening
text file in my VBA to read. After that i want to Add new
line, so i Open it for output mode, (I will create delete
existing and create new file that is ok for me). Here is
my code

Open sXferFile For Output As #1
While Not EOF(1)
Line Input #1, stemp
sFileName = stemp
Line Input #1, stemp
' sProcessDate = CDate(STEMP)
sProcessDate = stemp
Wend
close #1
-- after this
sfnum = freefile
Open sXferFile For Output As #sFnum
write #sfnum, "Over"
close #sfnum


I am getting Path/file error when i open it after closing
text file.

Need help?

Thanks
kalyan
 
R

Richard Mogy

First issue --- You can't read (line input #1) an output, you can only read
files open for input.
 
G

Guest

Hi

Sorry I wrongly pasted my code actual code will be like
this

FOLLOWING PART WORK FINE

Open sXferFile For input As #1
While Not EOF(1)
Line Input #1, stemp
sFileName = stemp
Line Input #1, stemp
sProcessDate = stemp
Wend
close #1

AFTER CLOSE, WHEN I OPEN FOR OUTPUT I GOT ERROR

sfnum = freefile
Open sXferFile For Output As #sFnum
write #sfnum, "Over"
close #sfnum

I KNOW IF I OPEN EXISTING FILE FOR OUTPUT, THAT WILL
DELETE AND RECREATE THAT IS FINE, BUT IT IS NOT BEHAVING
LIKE THAT, SIMPLY GIVE ERROR Path/file error


THANKS
KALYAN
 
R

Richard Mogy

And how is freefile populated?
Hi

Sorry I wrongly pasted my code actual code will be like
this

FOLLOWING PART WORK FINE

Open sXferFile For input As #1
While Not EOF(1)
Line Input #1, stemp
sFileName = stemp
Line Input #1, stemp
sProcessDate = stemp
Wend
close #1

AFTER CLOSE, WHEN I OPEN FOR OUTPUT I GOT ERROR

sfnum = freefile
Open sXferFile For Output As #sFnum
write #sfnum, "Over"
close #sfnum

I KNOW IF I OPEN EXISTING FILE FOR OUTPUT, THAT WILL
DELETE AND RECREATE THAT IS FINE, BUT IT IS NOT BEHAVING
LIKE THAT, SIMPLY GIVE ERROR Path/file error


THANKS
KALYAN
 

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