I want to make a program that edits a text file every time you open it
Here is what I have:
Dim firstNameInput
Dim lastNameInput
firstNameInput = inputbox("Please enter your name")
lastNameInput = inputbox("Enter your last name")
The rest of the code has to edit 1 specific text file: C:\Users\Me\Desktop\Edit.txt
then write the name and last name on a new line
If there is anyone who could help I would be great full, thanks.
Try something like that to append data :
Option Explicit
Const ForAppending = 8
Dim ws,fso,RootFolder,MyFile,firstNameInput,lastNameInput,fileStream
Do
firstNameInput = inputbox("Please enter your name")
Loop Until firstNameInput <> ""
Do
lastNameInput = inputbox("Enter your last name")
Loop Until lastNameInput <> ""
Set Ws = CreateObject("Wscript.Shell")
RootFolder = Ws.ExpandEnvironmentStrings("%USERPROFILE%\Desktop")
MyFile = RootFolder & "\Edit.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fileStream = fso.OpenTextFile(MyFile,ForAppending,True)
fileStream.WriteLine String(50,"*")
fileStream.WriteLine "First name: " & firstNameInput
fileStream.WriteLine "Last name: " & lastNameInput
fileStream.Close
ws.run DblQuote(MyFile)
'*****************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*****************************************
Another version. Just give a try !
Option Explicit
Const ForAppending = 8
Dim ws,fso,RootFolder,MyFile,firstNameInput,lastNameInput,fileStream
Dim Question,Title
Title = "Put Informations in file"
Set Ws = CreateObject("Wscript.Shell")
Call MyInputBox()
Do
Question = MsgBox("Did you want to add another data to file",VbYesNo+VbQuestion,Title)
If Question = VbYes Then
Call MyInputBox()
Else
ws.run DblQuote(MyFile)
Wscript.Quit()
End If
Loop Until Question = VbNo
'********************************************************************
Sub MyInputBox()
Do
firstNameInput = inputbox("Please enter your name")
Loop Until firstNameInput <> ""
Do
lastNameInput = inputbox("Enter your last name")
Loop Until lastNameInput <> ""
RootFolder = Ws.ExpandEnvironmentStrings("%USERPROFILE%\Desktop")
MyFile = RootFolder & "\Edit.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fileStream = fso.OpenTextFile(MyFile,ForAppending,True)
fileStream.WriteLine "First name: " & firstNameInput
fileStream.WriteLine "Last name: " & lastNameInput
fileStream.WriteLine String(30,"*")
fileStream.Close
End Sub
'********************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'********************************************************************
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments