1

I made a very simple text-to-speech program using VBScript (which I have limited experience with). It simply opens a command prompt, you type something, and Windows spvoice speaks the message.

I want this app to take the message input and add that text to a separate .txt file. So basically, this would be text-to-speech-to-text. Is VBScript too limited to read and write to text file?

Dim message, sapi
  message=InputBox("Enter text:","Enter Text")
  Set sapi=CreateObject("sapi.spvoice")
  sapi.Speak message

2 Answers 2

2

You could use the following to write the message to a text file.

Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile _ 
("C:\SpeechMessage.txt",2,true)
objFileToWrite.WriteLine(message)
objFileToWrite.Close
Set objFileToWrite = Nothing

VBScript Text Files: Read, Write, Append

EDIT:

Create the following to avoid permissions issues on the root of C:

C:\Speech\

And Try this code that will create the file if it does not exist and append to it if it does.

Set objFileToWrite = CreateObject("Scripting.FileSystemObject")
If objFileToWrite.FileExists("C:\Speech\Message.txt")  then
       Set objFileToWrite = objFileToWrite.OpenTextFile("C:\Speech\SpeechMessage.txt",8,true)
Else 
      Set objFileToWrite = objFileToWrite.CreateTextFile("C:\Speech\SpeechMessage.txt")   
End If 
objFileToWrite.WriteLine(message)
objFileToWrite.Close
Set objFileToWrite = Nothing
8
  • I'm getting a "permission denied" error. Would i have to run as administrator?
    – Nikolajs
    Commented Apr 15, 2021 at 13:40
  • to be more specific, i'm getting 800A0046 error
    – Nikolajs
    Commented Apr 15, 2021 at 13:57
  • @Nikolajs in this case the VBS does not create the file, can you confirm C:\SpeechMessage.txt exists and that you have full permissions to access it?
    – Moir
    Commented Apr 15, 2021 at 14:00
  • 1
    Why don't you suggest relative paths (which are almost always writable)?
    – iBug
    Commented Apr 15, 2021 at 14:20
  • 1
    that seemed to work. weird that it wasn't working before
    – Nikolajs
    Commented Apr 15, 2021 at 14:20
1

This seemed to work. I was running into issues with 800A0046 error at first. To fix this, I made a new folder in the C: drive and rooted to that.

Dim message, sapi, objFileToWrite

message=InputBox("What do you want me to say?","Speak to Me")
Set sapi=CreateObject("sapi.spvoice")
sapi.Speak message

Set objFileToWrite = 
CreateObject("Scripting.FileSystemObject")
_.OpenTextFile("C:\Speech\LiveChat.txt",2,true)
objFileToWrite.WriteLine(message)
objFileToWrite.Close
Set objFileToWrite = Nothing

Not the answer you're looking for? Browse other questions tagged or ask your own question.