Error 55 File Already Open

K

Keith

When I run the following code, I receive the following runt-time error:

Run-time error '55':

File already open

####################code

Public Sub InventoryCSVProcessing()

Dim filenumin As Integer, filenumout As Integer, filenuminraw As Integer,
filenumoutraw As Integer

Dim InputString As String
Dim intCount As Integer

filenuminraw = FreeFile
filenumoutraw = FreeFile

Open "x:\xlapps\cessna\inventory.csv" For Input As filenuminraw

'Read first 7 lines to skip the header junk

'For intCount = 1 To 7
' Line Input #filenumin, InputString
'Next intCount

Line Input #filenuminraw, InputString
InputString = Replace(InputString, Chr(10), Chr(13) & Chr(10))
Debug.Print InputString

If Dir("x:\xlapps\cessna\inventoryraw.csv") <> "" Then
Kill "x:\xlapps\cessna\inventoryraw.csv"
End If

'Error occurs on following line
Open "x:\xlapps\cessna\inventoryraw.csv" For Output As #filenumoutraw

Print #filenumoutraw, InputString
Close #filenumoutraw
Close #filenuminraw

######################end of code

I tried adding a 15 second delay before opening the file for output, and it
worked one time. It think the problem is the Kill statement, but I need to
delete the existing file before I write data.

Have anyone else seen this behavior? Any thoughts on how to fix it?

Thanks,

Keith
 
D

Dirk Goldgar

Keith said:
When I run the following code, I receive the following runt-time error:

Run-time error '55':

File already open

####################code

Public Sub InventoryCSVProcessing()

Dim filenumin As Integer, filenumout As Integer, filenuminraw As Integer,
filenumoutraw As Integer

Dim InputString As String
Dim intCount As Integer

filenuminraw = FreeFile
filenumoutraw = FreeFile

Open "x:\xlapps\cessna\inventory.csv" For Input As filenuminraw

'Read first 7 lines to skip the header junk

'For intCount = 1 To 7
' Line Input #filenumin, InputString
'Next intCount

Line Input #filenuminraw, InputString
InputString = Replace(InputString, Chr(10), Chr(13) & Chr(10))
Debug.Print InputString

If Dir("x:\xlapps\cessna\inventoryraw.csv") <> "" Then
Kill "x:\xlapps\cessna\inventoryraw.csv"
End If

'Error occurs on following line
Open "x:\xlapps\cessna\inventoryraw.csv" For Output As #filenumoutraw

Print #filenumoutraw, InputString
Close #filenumoutraw
Close #filenuminraw

######################end of code

I tried adding a 15 second delay before opening the file for output, and
it
worked one time. It think the problem is the Kill statement, but I need
to
delete the existing file before I write data.

Have anyone else seen this behavior? Any thoughts on how to fix it?


I think you could be getting the error because filenuminraw and
filenumoutraw will both get the same file number, since you don't open the
one before asking for the other. You have this:
filenuminraw = FreeFile
filenumoutraw = FreeFile

Open "x:\xlapps\cessna\inventory.csv" For Input As filenuminraw

Try this:

filenuminraw = FreeFile
Open "x:\xlapps\cessna\inventory.csv" For Input As filenuminraw
filenumoutraw = FreeFile
 

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