如何实现VBS输入框值存储及重启后作为默认值加载?
How to Pre-Fill InputBox with Last Saved Value in VBS
Got it, let's tweak your VBS script to remember the last value you entered and automatically populate it in the InputBox when you run the script again. Here's the step-by-step solution:
Updated Script
Dim objFSO, objTS Const ForReading = 1, ForWriting = 2 Set objFSO = CreateObject("Scripting.FileSystemObject") Dim lastValue, newInput Dim filePath : filePath = "C:\Users\UserName\Desktop\TextFile.txt" ' Check if the text file exists to retrieve the last saved value If objFSO.FileExists(filePath) Then Set objTS = objFSO.OpenTextFile(filePath, ForReading) lastValue = objTS.ReadAll() objTS.Close() Else ' Set a default prompt if the file doesn't exist yet lastValue = "Enter your value here" End If ' Show InputBox with the last saved value as default newInput = InputBox("Here it is", "Input Prompt", lastValue) ' Write the new input back to the file (overwrites old content) Set objTS = objFSO.OpenTextFile(filePath, ForWriting, True) objTS.Write(newInput) objTS.Close() ' Clean up object references Set objFSO = Nothing Set objTS = Nothing
What Changed & Why
- Added the
ForReadingconstant to handle reading the existing file - Added a file existence check:
- If the text file exists, we read its full content into
lastValue - If it's the first run (file doesn't exist), we use a friendly placeholder as the default
- If the text file exists, we read its full content into
- Modified the
InputBoxcall to passlastValueas the third parameter—this is what pre-fills the input field - Added cleanup for object references to free up system resources
Now every time you launch the script, it will pull your last saved value and drop it right into the input box. You can either use that value as-is or type a new one, which will then be saved for the next run.
内容的提问来源于stack exchange,提问作者masontm
相关产品推荐
相关产品推荐

