User FAQs

If you have a question, please send it to us.

1) What function would you use to determine if a file exists ?
You can use the DIR function to try to return the name of a file or directory.

If (VBA.Dir("C:\temp\myfile.xla") <> VBA.Constants.vbNullString) Then 
   Call VBA.MsgBox("Exists")
End If

You could also use the Microsoft.Scripting.Runtime library.

Set fso = New Scripting.FileSystemObject 
If (fso.FileExists("C:\temp\myfile.txt") = True) Then
   Call VBA.MsgBox("Exists")
End If

2) Write code to write data to a text file using a TextStream ?

Dim fso As Scripting.FileSystemObject 
Dim txTextStream As Scripting.TextStream
Set fso = New Scripting.FileSystemObject

Set txTextStream = fso.CreateTextFile("C:\temp\myfile.txt", True)
txTextStream.Write("some text")
txTextStream.WriteLine("some more text")

Set txTextStream = Nothing
Set fso = Nothing

3) Write code to read data from a text file using a TextStream ?

Dim sMyText As String 
Dim sMyText2 As String

If (fso.FileExists("C:\temp\myfile.text") = False) Then Exit Sub
Set txTextStream = fso.OpenTextFile("C:\temp\myfile.txt", _
   ForReading, True, TriStateUseDefault)

Do Until (txTextStream.AtEndOfStream = True)
   sMyText = sMyText & txTextStream.ReadLine

sMyText2 = txTextStream.ReadAll
Debug.Print sMyText = sMyText2 'True


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext