0
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

7
  • 1
    A simply search would have given you relevant answers. stackoverflow.com/questions/2198810/… Commented Dec 13, 2016 at 12:15
  • 1
    in his code he wants to use Early Binding, while your link shows an example of late binding Commented Dec 13, 2016 at 12:18
  • 1
    you are using early binding, therefore you need to set reference to the Microsoft Scripting Runtime library Commented Dec 13, 2016 at 12:19
  • new to vba, could you please explain how can i set reference to the Microsoft Scripting Runtime library Commented Dec 13, 2016 at 12:49
  • @PankajJaju - simple search was done, and the link you shared i have tried that code as well same error Commented Dec 13, 2016 at 12:53

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

no problem, don't forget to mark solved it it helped you.
how can i do that, new to website.
You click the "tick" :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.