1

I am working on a project and I am writing to a file from another file, but I want a .VBS file to say it like TTS. here is the code for that... But

Dim message, sapi
Set sapi=CreateObject("This Text") 
sapi.Speak message

And then the words "This Text" will come out of the speakers.

But, I don't want the words "This Text" to come out, I want it to say the words inside a .txt file (tts_text.txt)

So it needs to read a text file and store that in a variable and then the tts should read and say the variable.

2
  • stackoverflow.com/questions/854975/…
    – aphoria
    Commented Oct 4, 2017 at 14:05
  • I've just noticed you've deleted your most recent question - I don't think deleting it was necessary. I worked out why you added what appeared to be bot spam at the end - you had triggered an editor message requiring more detail, and so decided to post the warning message into the question. That was confusing! If this happens again, please think of what other detail you could add - in that particular case, it needed detail of what you had tried.
    – halfer
    Commented Nov 7, 2017 at 21:19

2 Answers 2

1

Use this to read/learn about the objects and their capabilities:

Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim goVC : Set goVC = CreateObject("SAPI.SpVoice")
goVC.Speak goFS.OpenTextFile(WScript.ScriptFullName).ReadAll()
2
  • thank you, but where does it open tts_text.txt ? sorry im new to vbs
    – GNY
    Commented Oct 4, 2017 at 14:14
  • i want it to open and read and then tts say a specific txt file, its tts_text.txt
    – GNY
    Commented Oct 4, 2017 at 14:20
1

You can give a try for this vbscript example :

Option Explicit
Dim Contents,File,message
File = "c:\tts_text.txt"
Contents = "It didn’t work after mass shootings at a nightclub in Orlando,"&_
"college campuses in Virginia and Oregon, a church in Charleston,"&_
"or at a movie theater and high school in Colorado."&_
"Or after two lawmakers survived assassination attempts." & vbcrlf &_
"But after a gunman killed 58 people and wounded more than 500 at a Las Vegas concert," & vbcrlf &_
"Democrats are going to try again to revamp the nation’s gun laws."
' We write this contents to the file
WriteTextFile Contents, file, 0
' We read the file contents and we store it into a variable message
message = ReadFileText(File)
' Now we can speak this message with SAPI object
Speak_from_File message
'**********************************************************
Sub Speak_from_File(message)
Dim Voice
Set Voice = CreateObject("SAPI.SpVoice")
Voice.Volume = 100
Voice.Rate = 0
Voice.Speak message
End Sub
'**********************************************************
Sub WriteTextFile(sContent, sPath, lFormat)
'lFormat -2 - System default, -1 - Unicode, 0 - ASCII
With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath,2,True,lFormat)
    .WriteLine sContent
    .Close
End With
End Sub
'**********************************************************
Function ReadFileText(sFile)
    Dim objFSO,oTS,sText
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set oTS = objFSO.OpenTextFile(sFile,1)
    sText = oTS.ReadAll
    oTS.close
    set oTS = nothing
    Set objFSO = nothing
    ReadFileText = sText
End Function 
'**********************************************************

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