PDA

View Full Version : [SOLVED] Error msg: File is already opened



Dreamer
07-03-2005, 07:23 AM
Hi all, Could anyone help me to figure out what's the problem?


With Activesheet
sPath = "C:\"
For I = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
sText = .Range("D" & I)
itext = FreeFile
sOutput = sPath & Trim(.Name) & ".Txt"
On Error Resume Next
Kill sOutput
On Error GoTo 0
Open sOutput For Output As #iText <--- It pops up a message telling me that the file is already opened.
Print #iText, sText
Next I
End With
Close #iText
Application.ScreenUpdating = True
End Sub

Norie
07-03-2005, 07:32 AM
Does this always happen? Have you tried running the code again?

Is there any other code?

Dreamer
07-03-2005, 07:36 AM
I don't think so... That's all the codes....:dunno

Norie
07-03-2005, 07:41 AM
Have you tried running it again?

sheeeng
07-03-2005, 07:51 AM
I tried that in my Excel...Nothing happens....No errors...
Can you upload your file for us?

Dreamer
07-03-2005, 09:27 AM
It works now... ! Thanks!

Bob Phillips
07-03-2005, 09:38 AM
How does it work. It fails for me.

If you have mor than one entry in A2:A65536 it wil fail, beacuse you open the same file number each time, or try to. It can only work if A3 down are empty.

And why would you kill the file every iteration of the loop anyway?

Norie
07-03-2005, 10:08 AM
because you open the same file number each time, or try to.

The filenumber actually changes on each iteration, try stepping through.

I think the problem is that the file isn't being closed.

This works for me.


With ActiveSheet

sPath = "C:\"

For I = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row

sText = .Range("D" & I)

iText = FreeFile

sOutput = sPath & Trim(.Name) & ".Txt"

On Error Resume Next
Kill sOutput
On Error GoTo 0

Open sOutput For Output As #iText
Print #iText, sText
Close #iText
Next I

Dreamer
07-03-2005, 10:09 AM
Open the same file number each time? which sentence tells this?
I know it's the looping problem, but I may not understand the meaning behinds the codes very clearly....

Norie
07-03-2005, 10:16 AM
Dreamer

What are you actually trying to do with this code?

As far as I can see it creates a file, writes a value to it then deletes the file, creates the file again, writes a value to it, deletes it etc etc.

Dreamer
07-03-2005, 10:26 AM
I try to kill any existing file before opening any files..
that's why I write that kill statement..

I wonder how can I close the file... so that I can prevent error message...

Bob Phillips
07-03-2005, 10:43 AM
The filenumber actually changes on each iteration, try stepping through.

I think the problem is that the file isn't being closed.

Yolu are rfght, I didn't notice that, I just got the same eror as the OP.

If the file number is upped, the fact that the previous file is till open shouldn't matter, it is all done by file number?

Bob Phillips
07-03-2005, 10:44 AM
I wonder how can I close the file... so that I can prevent error message...

Just close it


Close #iText

Norie
07-03-2005, 10:58 AM
If the file number is upped, the fact that the previous file is till open shouldn't matter, it is all done by file number?

I don't actually think that's the way it works.