如下代碼可朗讀指定文本文件中的內容,中英文皆可。測試
Dim fso,openFile,voice,str,filePath,fileCharset 'filePath爲要朗讀的文本文件路徑,根據實際替換 filePath = "D:\測試.txt" 'fileCharset爲要朗讀的文本文件編碼,根據實際替換 fileCharset = "utf-8" Set fso = CreateObject("Scripting.FileSystemObject") Set voice = CreateObject("SAPI.SpVoice") Set openFile = fso.OpenTextFile(filePath, 1, True) '將從文本中讀取的文字存入Str str = ReadFile(filePath, fileCharset) '朗讀文字 voice.Speak str '做用:從文件中讀取文本 '參數: 'filePath:文件路徑 'charSet:文件編碼格式 Function ReadFile(filePath, charSet) Dim Str Set stm = CreateObject("Adodb.Stream") stm.Type = 2 stm.mode = 3 stm.charset = charSet stm.Open stm.loadfromfile filePath Str = stm.readtext stm.Close Set stm = Nothing ReadFile = Str End Function
使用方法:編碼