Search code examples
vbaiosubroutine

Error 506: Class not defined: 'FileSystemObject'


I'm writing a VBA script for a program called "ScorBase". I'm trying to call a subroutine that creates a text file yet I"m encountering this error "506".

this is the code that I'm writhing:

    Sub emailFile()

' Declare a TextStream.
Dim stream 
'As TextStream

dim fso
Set fso = New FileSystemObject

' Create a TextStream.
Set stream = fso.CreateTextFile("C:\Users\eladt\Desktop\creatFile\Mail.txt", True)

stream.WriteLine "user Email."
stream.WriteLine "Maki"
stream.WriteLine "Nigeri"
stream.WriteLine "Sashimi"
stream.Close


End Sub

Solution

  • An much simpler approach without using any external references:

    Sub MM_Email_To_File()
    
    Dim FF As Integer
    FF = FreeFile
    
    '// The file will be created if it doesn't exist
    Open "C:\Users\eladt\Desktop\creatFile\Mail.txt" For Output As #FF
    
        Print #FF, "User Email"
        Print #FF, "Maki"
        Print #FF, "Nigeri"
        Print #FF, "Sashimi"
    
    Close #FF
    
    End Sub
    

    You can check out this MSDN article for more information on I/O operation in VBA.