How do I use FileSystemObject in VBA?

前端 未结 5 852
萌比男神i
萌比男神i 2020-11-22 02:50

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

5条回答
  •  攒了一身酷
    2020-11-22 03:45

    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

提交回复
热议问题