Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Open FileName For Input As #FileNum2 - - - File Already Open!

Hi,

I wrote the following script to parse all of my text files in a
specific folder into one. It is not working though. When it gets to
this line: Open FileName For Input As #FileNum2 , It tells me the
file is already open. I restarted my computer and I don't see it
open. I have at the end of the script Close #FileNum2 which I used
when the code was interrupted to try and close the file. How should I
be doing this or why would it be giving me this error?

Thanks for your help.

Sub parsedatafiles()

Dim ObjFSO As Object
Dim ObjFolder As Object
Dim ColFiles As Object
Dim objFile As Object
Dim FolderPath As String
Dim FileNum As Integer
Dim FileNum2 As Integer
Dim FileName As String

FileNum = FreeFile
FileNum2 = FreeFile
FolderPath = "Q:\DropBox\csv Files\Reject Enter\"

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = ObjFSO.getfolder(FolderPath)
Set ColFiles = ObjFolder.Files

Open FolderPath & "MyFileToAppend.Txt" For Append As #FileNum


For Each objFile In ColFiles
FileName = FolderPath & objFile.Name
Open FileName For Input As #FileNum2
Do Until EOF(FileNum2)
Line Input #FileNum2, Data
Write #FileNum, Data
Loop
Close #FileNum2
Next

Close #FileNum
Close #FileNum2

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Open FileName For Input As #FileNum2 - - - File Already Open!

Hi,

When you create the 2 filenums with freefile they are the same. Dont create
the second filenum until after you have used the first, See slight change to
code below.

Dim FolderPath As String
Dim FileNum As Integer
Dim FileNum2 As Integer
Dim FileName As String

FileNum = FreeFile
FolderPath = "Q:\DropBox\csv Files\Reject Enter\"

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = ObjFSO.getfolder(FolderPath)
Set ColFiles = ObjFolder.Files
Open FolderPath & "MyFileToAppend.Txt" For Append As #FileNum
For Each objFile In ColFiles
FileNum2 = FreeFile
FileName = FolderPath & objFile.Name
Open FileName For Input As #FileNum2
Do Until EOF(FileNum2)
Line Input #FileNum2, Data
Write #FileNum, Data
Loop
Close #FileNum2
Next

Close #FileNum
Close #FileNum2

End Sub
" wrote:

Hi,

I wrote the following script to parse all of my text files in a
specific folder into one. It is not working though. When it gets to
this line: Open FileName For Input As #FileNum2 , It tells me the
file is already open. I restarted my computer and I don't see it
open. I have at the end of the script Close #FileNum2 which I used
when the code was interrupted to try and close the file. How should I
be doing this or why would it be giving me this error?

Thanks for your help.

Sub parsedatafiles()

Dim ObjFSO As Object
Dim ObjFolder As Object
Dim ColFiles As Object
Dim objFile As Object
Dim FolderPath As String
Dim FileNum As Integer
Dim FileNum2 As Integer
Dim FileName As String

FileNum = FreeFile
FileNum2 = FreeFile
FolderPath = "Q:\DropBox\csv Files\Reject Enter\"

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = ObjFSO.getfolder(FolderPath)
Set ColFiles = ObjFolder.Files

Open FolderPath & "MyFileToAppend.Txt" For Append As #FileNum


For Each objFile In ColFiles
FileName = FolderPath & objFile.Name
Open FileName For Input As #FileNum2
Do Until EOF(FileNum2)
Line Input #FileNum2, Data
Write #FileNum, Data
Loop
Close #FileNum2
Next

Close #FileNum
Close #FileNum2

End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find filename of open file Ian[_4_] Excel Programming 5 April 23rd 08 06:10 PM
Excel wants to save an open file to another filename in VBA script BPoppe Excel Programming 0 November 9th 07 09:24 AM
Open File based on filename in cell TOMB Excel Programming 4 April 26th 07 07:55 PM
Macro to open most recent file with a particular filename string kwiklearner Excel Discussion (Misc queries) 1 August 23rd 06 01:24 AM
Why doesn't the File Open list sort into filename order? cycler New Users to Excel 7 July 16th 05 06:44 PM


All times are GMT +1. The time now is 04:21 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"