Handling Text files through VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello every body, i am just a begineer in VBA, I working on a Database in
MS-ACCESS, and i need to copy text from almost 22 text files ( e.g. XYZ.txt
0) , into one text file and name of the file should be MASTER.txt.
Please can any body help/guide/assist me in this regard
 
Sheraz said:
Hello every body, i am just a begineer in VBA, I working on a
Database in MS-ACCESS, and i need to copy text from almost 22 text
files ( e.g. XYZ.txt 0) , into one text file and name of the file
should be MASTER.txt.
Please can any body help/guide/assist me in this regard

There are several ways to do it, and you don't say whether you know the
names of all the input files in advance or whether you need to find that
out at run time (maybe by using the Dir() function).

You could have this procedure to append one input file to the output
file:

'----- start of code -----
'*** WARNING -- UNTESTED "AIR CODE" ***
Sub AppendTextFile(pstrInputPath As String, pstrOutputPath As String)

Dim intFileNoIn As Integer
Dim intFileNoOut As Integer
Dim strLine As String

intFileNoIn = FreeFile()
Open pstrInputPath For Input As #intFileNoIn

intFileNoOut = FreeFile()
Open pstrOutputPath For Append As #intFileNoOut

Do Until EOF(intFileNoIn)
Line Input intFileNoIn, strLine
Print #intFileNoOut, strLine
Loop

Close #intFileNoIn, #intFileNoOut

End Sub
'----- end of code -----

Then you could call this Sub repeatedly, each time passing the path to a
different input file and the path to the same output file.
 
Hi Dirk, thnx for the code, actually i do know the names of the files, anD we
can say that they are located in a folder s:\Exch Patch Status,
names are like JHM, NHM,AMT,A1P,A2P,WSI,WCI,......,
I hope the code will be changed in this case,, waiting fro a respone

regards,

Sheraz
 
Sheraz said:
Hi Dirk, thnx for the code, actually i do know the names of the
files, anD we can say that they are located in a folder s:\Exch Patch
Status,
names are like JHM, NHM,AMT,A1P,A2P,WSI,WCI,......,
I hope the code will be changed in this case,, waiting fro a respone

It's not a question of changing the code, because the Sub I posted is
designed to be called from another routine that provides the input and
output file paths. So, for example, with that Sub in place, you might
have another procedure that calls it for each file, like this:

'----- start of code for calling procedure -----
Sub AppendAllFiles()

Dim strFolder As String
Dim strOutputFile As String

strFolder = "s:\Exch Patch Status\"
strOutputFile = strFolder & "MASTER.txt"

AppendTextFile strFolder & "JHM.txt", strOutputFile
AppendTextFile strFolder & "NHM.txt", strOutputFile
AppendTextFile strFolder & "AMT.txt", strOutputFile
AppendTextFile strFolder & "A1P.txt", strOutputFile
AppendTextFile strFolder & "A2P.txt", strOutputFile
AppendTextFile strFolder & "WSI.txt", strOutputFile
AppendTextFile strFolder & "WCI.txt", strOutputFile
' ... and so on ...

End Sub
'----- end of code for calling procedure -----
 
Back
Top