Saturday, August 31, 2013

VBA: Using FileSystemObject to create a file

Sample code to create a file in VBA using FileSystemObject which is part of Microsoft Scripting Runtime. See this link on how to reference MSR.
Option Explicit

Sub TestMe()
    Dim fs As FileSystemObject
    Dim ts As TextStream
    Set fs = New FileSystemObject
    Set ts = fs.CreateTextFile("C:\tmp\file.txt", True)
    ts.WriteLine "Header1, Header2"
    ts.WriteLine "1," & CStr(DateTime.Now)
    Set ts = Nothing
    Set fs = Nothing
End Sub

No comments: