Consulting

Results 1 to 7 of 7

Thread: Edit textfile vba

  1. #1

    Edit textfile vba

    I have this code that creates a text file. More specifically Date, Time, etc. I want to replace only the 2nd line (previous Time with the Current Time). How can I do this.

    Thank you kindly

    Open OPath & OFile For Output As #1 
    Print  #1 , Date; vbNewLine; Time; vbNewLine ;tbName; vbNewLine; tbDepNm; (etc..)
    Close #1

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Dim p As String
    Dim s As String, ss As String
    Dim n As Long
    
    
    p = OPath & OFile
    
    Open p For Input As #1
        Do While Not EOF(1)
            Line Input #1, s
            n = n + 1
            If n = 2 Then s = Time
            ss = ss & s & vbNewLine
        Loop
    Close #1
    
    Open p For Output As #1
        Print #1, ss
    Close #1

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I would do something like this for long text files.
    Sub Prog2()  
      Dim oPath as String, oFile as String, iC As Integer, a as Variant
      
      oPath = ThisWorkbook.Path & "\"
      oFile = "ReplaceLine2.txt"
      If Dir(oPath & oFile) = "" Then Exit Sub
      
      iC = FreeFile
      Open oPath & oFile For Binary Access Read As #iC
        a = Split(Input(LOF(iC), iC), vbCrLf)
      Close #iC
      
      If UBound(a) = 0 Then Exit Sub
      
      iC = FreeFile
      Open oPath & oFile For Output Access Write As #iC
        a(1) = Time
        Print #iC, Join(a, vbCrLf)
      Close #iC
      
      Shell "cmd /c NotePad " & """" & oPath & oFile & """", vbHide
    End Sub

  4. #4
    I put this code in my workbook open and workbook close events. When the workbook opens, the code is to edit the second entry (which it does exactly what it is suppose to do), however, when I close the workbook, instead of editing the second entry, it enters duplicate entries. The code is only to edit the second entry of the text file when the workbook closes.

    Can someone help fix this problem.


        Set fs = CreateObject("Scripting.FileSystemObject")    
        f = fs.OpenTextFile(OPath & TmEvnt).readall
        pTime = Trim(Split(f, vbNewLine)(1))
        CurrentTime = Format(Now, "#0.#########0")
        
            Open OPath & TmEvnt For Input As #1 
            Do While Not EOF(1)
                Line Input #1 , s
                n = n + 1
                If n = 2 Then s = CurrentTime
                ss = ss & s & vbNewLine
            Loop
            Close #1 
            
            Open OPath & TmEvnt For Output As #1 
            Print #1 , ss         
            Close #1

  5. #5

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Did you try my method?

  7. #7
    I found my problem accidentally when I discover my Dim variables were place at the top of all the code instead of placing them in each of the Workbook Open event and in the Workbook Closed Event. Now it works as expected. Thank you , I greatly appreciated this forum for all the help.

Posting Permissions

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