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
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
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
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
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
Did you try my method?
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.