[英]VBA code to read a word doc
有什么方法可以創建一個 VBA 代碼來通過音頻閱讀 word 文檔? 就好像計算機正在閱讀文檔上的內容一樣。 (當然是所有文字)。
我在谷歌上找到了這個搜索。 http://vbadud.blogspot.com/2010/06/vba-how-to-convert-text-file-to-speech.html
Sub Speech_FromFile_Example()
Dim oVoice As SpVoice
Voice Object Dim oVoiceFile As SpFileStream
File Stream Object Dim sFile As String
File Name Set oVoice = New SpVoice
Set oVoiceFile = New SpFileStream
oVoice.Speak "This is an example for reading out a file"
sFile = "C:\ShasurData\ForBlogger\SpeechSample.txt"
oVoiceFile.Open sFile
oVoice.SpeakStream oVoiceFile
End Sub
代碼需要 Microsoft Speech Object Library
Word 的 VBA 宏:
Dim speech as SpVoice
Sub SpeakText()
On Error Resume Next
Set speech = New SpVoice
If Len(Selection.Text) > 1 Then 'speak selection
speech.Speak Selection.Text
SVSFlagsAsync + SVSFPurgeBeforeSpeak
Else 'speak whole document
speech.Speak ActiveDocument.Range(0, ActiveDocument.Characters.Count).Text
SVSFlagsAsync + SVSFPurgeBeforeSpeak
End If
Do
DoEvents
Loop Until speech.WaitUntilDone(10)
Set speech = Nothing
End Sub
Sub StopSpeaking()
'Based on a macro by Mathew Heikkila
'used to interrupt any running speech to text
On Error Resume Next
speech.Speak vbNullString, SVSFPurgeBeforeSpeak
Set speech = Nothing
End Sub
如果您使用的是 Windows 10,請小心 - 我有兩個同名的庫“Microsoft Speech Object Library” - 確保選擇指向 Windows/system32/Speech/Common/sapi.dll 的庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.