如下代码可朗读指定文本文件中的内容,中英文皆可。测试
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
使用方法:编码