vba macro save file code example

Example: excel vba save file txt

' Needs to add "Microsoft Scripting Runtime" reference to your file
Public Sub SaveTextToFile(ByVal pFile As String, ByVal pText As String)
    Dim fso As FileSystemObject
    Dim fileStream As TextStream

    Set fso = New FileSystemObject
    Set fileStream = fso.CreateTextFile(pFile)
    fileStream.WriteLine pText
    fileStream.Close
End Sub
' -----------------------------------------------------------------------
Sub TestMe()
      SaveTextToFile "C:\Temp\export.txt", "A sentence to be saved..."
End Sub

Tags:

Vb Example