In VBA How to Open two text file in same time?

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

Guest

Read data from one opened text file and write to another text file without
closing First text file.
 
Depends what you want to do, but here's one example:

Private Sub CommandButton1_Click()
Dim FileRead As Long
Dim FileWrite As Long
Dim TempStr As String

Const ReadFileName As String = "C:\ReadText.txt"
Const WriteFileName As String = "C:\WriteText.txt"

FileRead = FreeFile
Open ReadFileName For Input As #FileRead

FileWrite = FreeFile
Open WriteFileName For Output As #FileWrite
'Or maybe
'Open WriteFileName For Append As #FileWrite

Do While Not EOF(FileRead)
Input #FileRead, TempStr

'Do something with the string
TempStr = "Read value: " & TempStr

'Write out
Write #FileWrite, TempStr
'Or maybe use Print
Loop

Close #FileRead
Close #FileWrite

End Sub

NickHK
 
and here's another using textstream objects found from VBA help-files and modified it a bit:
Sub OpenTextFileTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fs, f, f2
Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.OpenTextFile("c:\testfile.txt", ForReading)
Set f2 = fs.OpenTextFile("c:\testfile2.txt", ForAppending)
f2.Write f.read(100)
f.Close
f2.Close
End Sub
 
NickHK said:
Depends what you want to do, but here's one example:

Private Sub CommandButton1_Click()
Dim FileRead As Long
Dim FileWrite As Long
Dim TempStr As String

Const ReadFileName As String = "C:\ReadText.txt"
Const WriteFileName As String = "C:\WriteText.txt"

FileRead = FreeFile
Open ReadFileName For Input As #FileRead

FileWrite = FreeFile
Open WriteFileName For Output As #FileWrite
'Or maybe
'Open WriteFileName For Append As #FileWrite

Do While Not EOF(FileRead)
Input #FileRead, TempStr

'Do something with the string
TempStr = "Read value: " & TempStr

'Write out
Write #FileWrite, TempStr
'Or maybe use Print
Loop

Close #FileRead
Close #FileWrite

End Sub

NickHK




Hi Nick HK,
Thanks alot ! Great!.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top