【问题标题】:Read from file using VBS and say the contents使用 VBS 读取文件并说出内容
【发布时间】:2018-03-15 23:09:49
【问题描述】:

我正在处理一个项目,我正在从另一个文件写入一个文件,但我想要一个 .VBS 文件来像 TTS 一样说它。这是代码......但是

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

然后扬声器中会出现“This Text”字样。

但是,我不希望出现“This Text”这个词,我希望它说出 .txt 文件 (tts_text.txt) 中的词

所以它需要读取一个文本文件并将其存储在一个变量中,然后 tts 应该读取并说出该变量。

【问题讨论】:

  • 我刚刚注意到you've deleted your most recent question - 我认为没有必要删除它。我弄清楚了为什么您在最后添加了似乎是机器人垃圾邮件的原因-您触发了需要更多详细信息的编辑器消息,因此决定将警告消息发布到问题中。那令人困惑!如果再次发生这种情况,请考虑您可以添加哪些其他细节 - 在这种特殊情况下,它需要您尝试过的细节。

标签: file vbscript text-to-speech


【解决方案1】:

使用它来阅读/了解对象及其功能:

Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim goVC : Set goVC = CreateObject("SAPI.SpVoice")
goVC.Speak goFS.OpenTextFile(WScript.ScriptFullName).ReadAll()

【讨论】:

  • 谢谢,但它在哪里打开 tts_text.txt ?抱歉,我是 vbs 的新手
  • 我希望它打开并阅读,然后 tts 说一个特定的 txt 文件,它的 tts_text.txt
【解决方案2】:

你可以试试这个 vbscript 例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多