Merging Text Files with Excel VBA

pingme89

Board Regular
Joined
Jan 23, 2014
Messages
176
Office Version
  1. 365
Platform
  1. Windows
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?

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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi pingme89,

you open the textfiles by
Rich (BB code):
      Open File For Input As InFile
while you close any other file with
Rich (BB code):
      Close TextFile
instead of what I would expect
Rich (BB code):
      Close InFile

Ciao,
Holger
 
Upvote 0
Hi pingme89,

you open the textfiles by
Rich (BB code):
      Open File For Input As InFile
while you close any other file with
Rich (BB code):
      Close TextFile
instead of what I would expect
Rich (BB code):
      Close InFile

Ciao,
Holger
Thanks but the error occurs before that line of code.
Fixing that won't solve the error.
 
Upvote 0
Hi pingme,

you have added the library "Microsoft Scripting Runtime" under Tools/References in the IDE?

Code works fine for me when changing the paths to my needs:

VBA Code:
Private Sub TestMerge_mod()
' https://www.mrexcel.com/board/threads/merging-text-files-with-excel-vba.1225621/

' needed additional library:   Microsoft Scripting Runtime

Dim FSO As FileSystemObject
Dim OutFile As Integer
Dim InFile As Integer
Dim FilePath As String
Dim OutPath As String
Dim InPath As String
Dim FileContent As String
Dim Folder As FileSystemObject.Folder
Dim File As FileSystemObject.File

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 InFile
Next File
Close OutFile

End Sub

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top