By l--''''''---------''''''''''''

2010-07-13 00:00:12 8 Comments

Is there something that I need to reference? How do I use this:

Dim fso As New FileSystemObject
Dim fld As Folder
Dim ts As TextStream

I am getting an error because it does not recognize these objects.


@Robert Mearns 2010-07-13 10:46:33

Within Excel you need to set a reference to the VB script run-time library. The relevant file is usually located at \Windows\System32\scrrun.dll

  • To reference this file, load the Visual Basic Editor (ALT+F11)
  • Select Tools > References from the drop-down menu
  • A listbox of available references will be displayed
  • Tick the check-box next to 'Microsoft Scripting Runtime'
  • The full name and path of the scrrun.dll file will be displayed below the listbox
  • Click on the OK button.

This can also be done directly in the code if access to the VBA object model has been enabled.

Access can be enabled by ticking the check-box Trust access to the VBA project object model found at File > Options > Trust Center > Trust Center Settings > Macro Settings

VBA Macro settings

To add a reference:

Sub Add_Reference()

    Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\scrrun.dll"
'Add a reference

End Sub

To remove a reference:

Sub Remove_Reference()

Dim oReference As Object

    Set oReference = Application.VBE.ActiveVBProject.References.Item("Scripting")

    Application.VBE.ActiveVBProject.References.Remove oReference
'Remove a reference

End Sub

@l--''''''---------'''''''''''' 2010-07-13 15:30:00

robert thank you very much

@Thomas Fankhauser 2013-02-24 13:50:51

What do you do on a Mac?

@LuizAngioletti 2015-08-06 22:03:43

Isn't there a way to do this via command line, as in including a library or something?

@compski 2017-04-05 06:49:14

Is there a way to AUTO tick the check-box 'Microsoft Scripting Runtime'? for ALL Excel files? I'm using Excel 2016

@Robert Mearns 2017-04-05 08:00:22

Have a look at the accepted answer to this question.…

@FIRE FOX 2018-05-04 16:18:49

After importing the scripting runtime as described above you have to make some slighty modification to get it working in Excel 2010 (my version). Into the following code I've also add the code used to the user to pick a file.

Dim intChoice As Integer
Dim strPath As String

' Select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False

' Show the selection window
intChoice = Application.FileDialog(msoFileDialogOpen).Show

' Get back the user option
If intChoice <> 0 Then
    strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
    Exit Sub
End If

Dim FSO As New Scripting.FileSystemObject
Dim fsoStream As Scripting.TextStream
Dim strLine As String

Set fsoStream = FSO.OpenTextFile(strPath)

Do Until fsoStream.AtEndOfStream = True
    strLine = fsoStream.ReadLine
    ' ... do your work ...

Set FSO = Nothing

Hope it help!

Best regards


@thedanotto 2018-02-15 23:25:01

After adding the reference, I had to use

Dim fso As New Scripting.FileSystemObject

@Stefano Spinucci 2016-07-31 19:54:53

In excel 2013 the object creation string is:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

instead of the code in the answer above:

Dim fs,fname
Set fs=Server.CreateObject("Scripting.FileSystemObject")

@Marcucciboy2 2018-07-18 13:47:45

to be slightly more explicit, I normally see it as Dim fso As Object

@Gerald Ferreira 2010-07-13 00:04:16

These guys have excellent examples of how to use the filesystem object

dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
fname.WriteLine("Hello World!")
set fname=nothing
set fs=nothing

@Spangen 2017-12-07 08:34:47

This code snippet demonstrates the use of the FSO in ASP/IIS not excel

Related Questions

Sponsored Content

42 Answered Questions

[SOLVED] How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?

  • 2008-09-29 22:30:28
  • mistrmark
  • 998087 View
  • 1732 Score
  • 42 Answer
  • Tags:   c# .net excel file-io

22 Answered Questions

[SOLVED] Is there a way to crack the password on an Excel VBA Project?

  • 2009-06-22 10:37:13
  • Jonathan Sayce
  • 1005119 View
  • 435 Score
  • 22 Answer
  • Tags:   excel vba passwords

13 Answered Questions

[SOLVED] How to avoid using Select in Excel VBA

  • 2012-05-23 05:57:58
  • 235028 View
  • 475 Score
  • 13 Answer
  • Tags:   excel vba

2 Answered Questions

[SOLVED] VBA - Access FileSystemObject without Dimming it as an object

  • 2017-06-29 14:31:22
  • EliasWick
  • 209 View
  • 1 Score
  • 2 Answer
  • Tags:   vba

2 Answered Questions

[SOLVED] Property Get doesn't keep value even if static

  • 2016-08-02 08:58:58
  • Julian Kuchlbauer
  • 77 View
  • 2 Score
  • 2 Answer
  • Tags:   vba excel-vba excel

1 Answered Questions

2 Answered Questions

[SOLVED] How to use the FileSystemObject in VBA

2 Answered Questions

[SOLVED] VBA, FileSystemObject, Windows sort order

1 Answered Questions

[SOLVED] Read function - FileSystemObject

2 Answered Questions

[SOLVED] trouble with using filesystemobject in vba

Sponsored Content