Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Find filename of open file | Excel Programming | |||
Excel wants to save an open file to another filename in VBA script | Excel Programming | |||
Open File based on filename in cell | Excel Programming | |||
Macro to open most recent file with a particular filename string | Excel Discussion (Misc queries) | |||
Why doesn't the File Open list sort into filename order? | New Users to Excel |