2017-10-04 216 views
1

我正在处理一个项目,并正在从另一个文件写入一个文件,但我想要一个.VBS文件来表示它像TTS。这里是该代码...但使用VBS从文件中读取并说出内容

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

然后写着“该文章”会来的音箱出来。

但是,我不想要的话“这个文本”出来的,我想可以说一个.txt文件中的话(tts_text.txt)

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

+0

https://stackoverflow.com/questions/ 854975/how-to-read-from-a-text-file-using-vbscript – aphoria

+0

我刚刚注意到[你已经删除了你最近的问题](https://stackoverflow.com/questions/47166956/have-两个javascript-slider-to-work-at-same-time) - 我不认为删除它是必要的。我想出了为什么你最终添加了似乎是机器人垃圾邮件的东西 - 你已经触发了一个需要更多细节的编辑信息,因此决定将警告信息发布到问题中。这很混乱!如果再次发生这种情况,请考虑您可以添加的其他细节 - 在这种特殊情况下,需要详细说明您尝试的内容。 – halfer

回答

1

使用该阅读/学习的对象和他们的能力:

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

谢谢,但它在哪里打开tts_text.txt? 对不起,我新vbs –

+0

我希望它打开和阅读,然后说tts一个特定的txt文件,它的tts_text.txt –

0

你可以给这个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 
'**********************************************************