Wednesday, 24 July 2013

Looping Through Files In A Directory(VBA)


When we want go through the files, located in directory, then we need loop in side the directory.
Below i write a  sample code for the same(Blue color text is main loop go to through the file in folder)-

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim objFolder
    Set objFolder = fso.GetFolder("C:\ForSample")       
    Dim i As Integer: i = 1
    Dim FileName As Variant
    Dim LineText As String
    Dim str As String
    Dim objFile
    For Each objFile In objFolder.files
        str = ""
         FileName = objFile
         Open FileName For Input As #50
         While Not EOF(50)
            Line Input #50, LineText
            str = str & LineText
         Wend       
        Close #50
    Next


No comments:

Post a Comment