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 To add a reference: Sub AddReference Application.VBE.ActiveVBProject.References.AddFromFile 'C: Windows System32 scrrun.dll' 'Add a reference End Sub To remove a reference: Sub RemoveReference Dim oReference As Object Set oReference = Application.VBE.ActiveVBProject.References.Item('Scripting') Application.VBE.ActiveVBProject.References.Remove oReference 'Remove a reference End Sub.
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) Else 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.
Loop fsoStream.Close Set FSO = Nothing Hope it help! Best regards Fabio.