PDA

View Full Version : [SOLVED] Edit textfile vba



av8tordude
08-09-2019, 10:22 PM
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 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1)
Print #1 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1) , Date; vbNewLine; Time; vbNewLine ;tbName; vbNewLine; tbDepNm; (etc..)
Close #1 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1)

mana
08-10-2019, 06:51 AM
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

Kenneth Hobs
08-10-2019, 09:27 AM
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

av8tordude
08-14-2019, 08:17 AM
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 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1)
Do While Not EOF(1)
Line Input #1 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1) , s
n = n + 1
If n = 2 Then s = CurrentTime
ss = ss & s & vbNewLine
Loop
Close #1 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1)

Open OPath & TmEvnt For Output As #1 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1)
Print #1 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1) , ss
Close #1 (https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1)

Fluff
08-14-2019, 08:50 AM
Cross posted
https://www.mrexcel.com/forum/excel-questions/1106938-edit-textfile-error.html

Kenneth Hobs
08-14-2019, 01:41 PM
Did you try my method?

av8tordude
08-15-2019, 05:46 PM
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.