Results 1 to 4 of 4
  1. #1
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    why this macro not cicling .... (2000 sr-1)

    why this code no cicling all file wit .epf and 3 characyer =TAB...?
    In effect i want to cicling all file in c:epf with .epf and append in one file named Appended.txt

    Option Explicit
    Sub AppendFiles()

    Dim FileText As String
    Dim Folder, TEST_SUBFILE As String
    Dim FSO As Object
    Dim MainFile
    Dim SubFile

    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

    MainFile = "Appended.txt"
    Folder = "C:EPF"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set MainFile = FSO.OpenTextFile(Folder & MainFile, ForAppending, True, TristateUseDefault)

    SubFile = Dir(Folder & "*.EPF")
    Do While SubFile <> ""
    If Left((SubFile), 3) = "TAB" Then
    Set SubFile = FSO.GetFile(SubFile)
    Set SubFile = SubFile.OpenAsTextStream(ForReading, TristateUseDefault)
    FileText = SubFile.ReadAll
    SubFile.Close
    MainFile.Write FileText
    SubFile = Dir
    End If
    Loop

    MainFile.Close

    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: why this macro not cicling .... (2000 sr-1)

    Perhaps the code gets confused because you're using SubFile both as a string and as a File object. Try adding an extra variable:

    Sub AppendFiles()
    Dim FileText As String
    Dim Folder As String
    Dim SubFile As String
    Dim FSO As Object
    Dim MainFile
    Dim fil As Object
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

    MainFile = "Appended.txt"
    Folder = "C:EPF"
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set MainFile = FSO.OpenTextFile(Folder & MainFile, ForAppending, True, TristateUseDefault)
    SubFile = Dir(Folder & "TAB*.EPF")
    Do While SubFile <> ""
    Set fil = FSO.GetFile(SubFile).OpenAsTextStream(ForReading, TristateUseDefault)
    FileText = fil.ReadAll
    fil.Close
    MainFile.Write FileText
    Debug.Print SubFile
    SubFile = Dir
    Loop
    MainFile.Close
    End Sub

  3. #3
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: why this macro not cicling .... (2000 sr-1)

    error "53" impossible to found file...(?!) in line:

    Set fil = FSO.GetFile(SubFile).OpenAsTextStream(ForReading, TristateUseDefault)

    sure in dir are presents many file named TAB*.epf


    peraphs solved with:

    Set fil = FSO.GetFile( Folder & SubFile ).OpenAsTextStream(ForReading, TristateUseDefault)

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: why this macro not cicling .... (2000 sr-1)

    That's better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •