I have created some code but for some reason, whenever a text file is looped, it generates an error saying the file is already open even though it is not.
Essentially I am trying to loop through all text files in a specified folder and copy all contents from all the files and combine them into 1 output file.
I have tried using Shell command to accomplish the same thing but the problem is my AntiVirus software thinks it is malicious and deletes the file. So I don't want to use the Shell command.
Here is my code. Can someone tell me what I am doing wrong?
Essentially I am trying to loop through all text files in a specified folder and copy all contents from all the files and combine them into 1 output file.
I have tried using Shell command to accomplish the same thing but the problem is my AntiVirus software thinks it is malicious and deletes the file. So I don't want to use the Shell command.
Here is my code. Can someone tell me what I am doing wrong?
VBA Code:
Private Sub TestMerge()
Dim FSO
Dim TextFile As Integer
Dim FilePath As String
Dim OutPath As String
Dim InPath As String
Dim FileContent As String
InPath = "C:\Users\phili\Desktop\Access Templates\AnkurDB_Code\"
Set FSO = New FileSystemObject
' get the directory you want
Set Folder = FSO.GetFolder(InPath)
'File Path of Text File
OutPath = "C:\Users\BB\Desktop\Access Templates\DB_Code\" & "Module1.txt"
'Determine the next file number available for use by the FileOpen function
OutFile = FreeFile
'Open the text file
Open OutPath For Output As OutFile
For Each File In Folder.Files
InFile = FreeFile
Open File For Input As InFile
If Right(File, 3) = "txt" Then
'Store file content inside a variable
FileContent = Input(LOF(InFile), InFile) ' LOCATION OF ERROR.
Print #OutFile, FileContent
End If
'Close Text File
Close TextFile
Next File
Close OutFile
End Sub