GNY
GNY

Reputation: 39

Read from file using VBS and say the contents

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.

Upvotes: 1

Views: 1067

Answers (2)

Hackoo
Hackoo

Reputation: 18827

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 
'**********************************************************

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

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()

Upvotes: 1

Related Questions