GAURAV CHHABRA
GAURAV CHHABRA

Reputation: 97

Create Text File using VBA

Private Sub Update()

    Dim myFileSystemObject As FileSystemObject
    Dim myFile As Object

    Set myFileSystemObject = New FileSystemObject

    Set myFile = myFileSystemObject.CreateTextFile("C:\Error.txt")

    myFile.WriteLine "Error"

    myFile.Close
    Set myFileSystemObject = Nothing

End Sub

I am getting the Error below:

Compiler Error: Expected : expression

Tried lot many things but not working

Upvotes: 0

Views: 11957

Answers (2)

Andrew Truckle
Andrew Truckle

Reputation: 19197

There is another way you can do it:

Dim hFile As Long

hFile = FreeFile

Open strFile For Output As #hFile

Print #hFile, "Line of text"

Close #hFile

This way you avoid needed to use the FileSystemObject.

Upvotes: 3

KyloRen
KyloRen

Reputation: 2741

Try this code, it will create the object for you and you can add data to the file as you need and then save it.

Private Sub Update()

    Dim fSo As Object
    Dim myFile As Object


       Set fSo = CreateObject("Scripting.FileSystemObject")

       Set myFile = fSo.CreateTextFile("c:\sample.txt", True)

       myFile.WriteLine "Error"

    myFile.Close
    Set fSo = Nothing

End Sub

Upvotes: 1

Related Questions