VB and VBA Users Source Code: Saving text to a file using the FileScriptingObject
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Saving text to a file using the FileScriptingObject
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, October 12, 2001
Hits:
1190
Category:
Files/Directories/IO
Article:
The following code demonstrates how to use the file scripting object to write to a text file. 'Purpose : Uses the file scripting object to save text to a file. 'Inputs : sPath The path and file name of the file. ' sText The text to save to the file. ' [lMode] 2 = For Writing ' 8 = For Appending 'Outputs : Returns True on success. 'Author : Andrew Baker 'Date : 08/01/2001 20:24 'Notes : Requires a Reference to SCRRUN.DLL ("Microsoft Scripting Runtime") 'Revisions : Function TextFileSaveFSO(sPath As String, sText As String, Optional lMode As Long = 2) As Boolean Dim oFSO As Scripting.FileSystemObject, oFile As Scripting.TextStream On Error GoTo ErrFailed Set oFSO = New Scripting.FileSystemObject Set oFile = oFSO.OpenTextFile(sPath, lMode, True) oFile.Write sText oFile.Close Set oFile = Nothing Set oFSO = Nothing TextFileSaveFSO = True Exit Function ErrFailed: Debug.Print "Error in TextFileSaveFSO: " & Err.Description TextFileSaveFSO = False End Function 'Writes "Test" into a file called aaaa.txt Sub Demonstration TextFileSaveFSO "c:\aaaa.txt", "Test" End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder