'How can I have PowerPoint VBA overwrite specific lines in a TXT file?

PowerPoint 365

I've used VBA on text files (creating, appending, and reading), but never to write over specific lines and re-save the file, and I'm lost on that.

I have a txt file, "C:\Activity.txt" that has the following 5 lines of content:

1/12/2022
3
2/4/2022
7
2/10/2022

Upon an action I take, code captures the action type (sType), and then I need it to read and write to that text file. Like...

If sType = "regular" then

'Read line #2
'Add 1 to that value
'Overwrite line #2 with that new value
'Overwrite line #3 with today's date
'Save it back as the same file.

Elseif sType = "special" then

'Read line #4
'Add 1 to that value
'Overwrite line #4 with that new value
'Overwrite line #5 with today's date
'Save it back as the same file.

End If



Solution 1:[1]

Leaning on methods I used in past projects dealing with TXT files, I composed the following, which works for me. Feel free to comment.

Sub ChangeTxt()

'========================================
'This is abridged from the actual routine.
'Previous to this code, it ensures the file exists,
'and if not creates it with 5 lines of text – Date() and 4 zeros
'========================================

'========================================
'Also, sTmpType is carried over from a button action
'in a callback via RibbonX in the actual routine.
'So, for testing purposes only...
'Set object for condition of type of action taken
   Dim sTmpType As String
   sTmpType = "Action2"
'========================================

'Set objects for the 5 lines of text in the TXT file
   Dim sLn1 As String
   Dim iLn2 As Long
   Dim sLn3 As String
   Dim iLn4 As Long
   Dim sLn5 As String
'Set the TXT file path
   Dim sPath As String
   sPath = "C:\MyFolder\Test.txt"
'Get today's date (as string is fine)
   Dim sToday As String
   sToday = Date
      
'>>> Get text content from TXT file
      
'Open text file (1 = read only)
   Dim fs, f
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set f = fs.OpenTextFile(sPath, 1)

'Read the text file line by line
   sLn1 = f.readline 'Date began tracking
   iLn2 = f.readline 'Frequency of Action1
   sLn3 = f.readline 'Last date Action1
   iLn4 = f.readline 'Frequency of Action2
   sLn5 = f.readline 'Last Date of Action2

'Close the txt file
   f.Close
   
'>>> Compose updated text content
   
'Set new data variables based on action type taken
   If sTmpType = "Action1" Then
   'Calculate new value
      iLn2 = iLn2 + 1
   'Get today's date
      sLn3 = sToday
      
   ElseIf sTmpType = "Action2" Then
   'Calculate new value
      iLn4 = iLn4 + 1
   'Get today's date
      sLn5 = sToday

   End If
   
'Create the new text content that will be written
   Dim sNewText As String
   sNewText = _
      sLn1 & vbCrLf & _
      iLn2 & vbCrLf & _
      sLn3 & vbCrLf & _
      iLn4 & vbCrLf & _
      sLn5 & vbCrLf

'>>> Overwrite file with updated content

'Open text file again for writing over it with new content (2 = replace content only)
   Set f = fs.OpenTextFile(sPath, 2)
   f.Write sNewText
   f.Close

End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mel Turco