How to create and write a textfile in VBA

To create and write a textfile in VBA you can use the following snippet.
This should also work when using VB6.

Sample VBA

Dim fso as Object
Dim oFile as Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.CreateTextFile("C:\Users\Codesnippets\Desktop\Test.txt")

oFile.WriteLine "This is a sample line!" 
oFile.Close

Set fso = Nothing
Set oFile = Nothing  

One thought on “How to create and write a textfile in VBA

Leave a Reply