Posted By

freesites on 10/31/07


Tagged

VB6VBScript


Versions (?)

Write to file in VB6/VBScript.


 / Published in: Other
 

This can be used to log errors from Macros etc.

  1. Private Sub WriteToFile()
  2.  
  3. 'File path is c:\testfile.txt
  4. 'Method 1
  5. Set fs = CreateObject("Scripting.FileSystemObject")
  6.  
  7. Set a = fs.CreateTextFile("c:\testfile.txt", True)
  8.  
  9. a.WriteLine ("This is a test.")
  10.  
  11. a.Close
  12.  
  13.  
  14.  
  15. 'Method 2
  16.  
  17.  
  18. Open "C:\TESTFILE.txt" For Output As #1 ' Open file for output.
  19.  
  20. Print #1, "This is a test" ' Print text to file.
  21.  
  22. Print #1, ' Print blank line to file.
  23.  
  24. Print #1, "Zone 1"; Tab; "Zone 2" ' Print in two print zones.
  25.  
  26. Print #1, "Hello"; " "; "World" ' Separate strings with space.
  27.  
  28. Print #1, Spc(5); "5 leading spaces " ' Print five leading spaces.
  29.  
  30. Print #1, Tab(10); "Hello" ' Print word at column 10.
  31.  
  32.  
  33.  
  34. ' Assign Boolean, Date, Null and Error values.
  35.  
  36. Dim MyBool, MyDate, MyNull, MyError
  37.  
  38. MyBool = False: MyDate = #2/12/1969#: MyNull = Null
  39.  
  40. MyError = CVErr(32767)
  41.  
  42. ' True, False, Null, and Error are translated using locale settings of
  43.  
  44. ' your system. Date literals are written using standard short date
  45.  
  46. ' format.
  47.  
  48. Print #1, MyBool; " is a Boolean value"
  49.  
  50. Print #1, MyDate; " is a date"
  51.  
  52. Print #1, MyNull; " is a null value"
  53.  
  54. Print #1, MyError; " is an error value"
  55.  
  56. Close #1 ' Close file.
  57.  
  58.  
  59. End Sub

Report this snippet  

You need to login to post a comment.