Copy the code below to a blank new module
for a solution in VBA. Set the Reference
as instructed below.
' This module requires a reference to the
' Microsoft Scripting Runtime Object Library.
' To set the reference, follow these steps:
' 1. In the VBA editor, open the TOOLS menu
' and select REFERENCES.
' 2. In the References dialog, select
' Microsoft Scripting Runtime.
' 3. Click the OK button to close the
' References dialog.
Private Sub CopyTextFiles()
Const strcPath As String = "C:\My Documents\"
Dim objFSO As Scripting.FileSystemObject
Dim objTSA As Scripting.TextStream
Dim objTSB As Scripting.TextStream
Dim objTSC As Scripting.TextStream
Dim strA As String
Dim strB As String
' Create an object variable pointing to
' a FileSystemObject:
Set objFSO = New Scripting.FileSystemObject
' Create object variables pointing to the
' two existing text files, A and B:
Set objTSA = objFSO.OpenTextFile(strcPath & "A.txt")
Set objTSB = objFSO.OpenTextFile(strcPath & "B.txt")
' Create an object variable pointing to
' a new text file, C, overwriting any existing file:
Set objTSC = objFSO.CreateTextFile(strcPath & "C.txt", True)
' Read files A and B:
strA = objTSA.ReadAll
strB = objTSB.ReadAll
' Write strA to file C:
objTSC.Write strA
' Add two blank lines to file C:
objTSC.WriteBlankLines 2
' Write strB to file C:
objTSC.Write strB
' Close the files:
objTSA.Close
objTSB.Close
objTSC.Close
' Destroy object variables:
Set objTSA = Nothing
Set objTSB = Nothing
Set objTSC = Nothing
Set objFSO = Nothing
End Sub
Regards
Geoff