2017-10-04 95 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 
'**********************************************************