Try...
Option Explicit
Sub Insert_FileHeaderAndFooter()
Dim sTextOut$, sFileIn$, sFileOut$ 'as sring
Const sHeader$ = "My,header,text" '..edit to suit
Const sFooter$ = "My,footer,text" '..edit to suit
'Get the file to open and the new file to SaveAs
sFileIn = Get_FileToOpen: sFileOut = Get_FileToSave
If sFileIn = "" Or sFileOut = "" Then Exit Sub
'Insert header/footer and write to the new file
sTextOut = sHeader & vbCrLf _
& ReadTextFileContents(sFileIn) _
& vbCrLf & sFooter
WriteTextFileContents sTextOut, sFileOut
End Sub
'//Helper functions
'''''''''''''''''''
Function ReadTextFileContents(Filename As String) As String
' Reads large amounts of data from a text file in one single step.
Dim iNum As Integer
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFileContents = Space$(LOF(iNum))
ReadTextFileContents = Input(LOF(iNum), iNum)
ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFileContents()
Sub WriteTextFileContents(ByVal TextOut As String, _
Filename As String, _
Optional AppendMode As Boolean = False)
' Writes/Overwrites or Appends large amounts of data to a Text file
' in one single step.
Dim iNum As Integer
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then TextOut = vbCrLf & TextOut
Open Filename For Output As #iNum: Print #iNum, TextOut;
ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFileContents()
Function Get_FileToOpen$(Optional FileTypes$)
' Returns the full path and filename of a file to open.
Dim v As Variant
If FileTypes = "" Then FileTypes = "All Files ""*.*"", (*.*)"
v = Application.GetOpenFilename(FileTypes)
If (Not v = False) Then Get_FileToOpen = v Else Get_FileToOpen = ""
End Function
Function Get_FileToSave$(Optional FileOut$)
' Returns the full path and filename of a file to save.
Dim v As Variant
v = Application.GetSaveAsFilename(FileOut)
If (Not v = False) Then Get_FileToSave = v Else Get_FileToSave = ""
End Function
--
Garry
Free usenet access at
http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion