用于读取单词doc的VBA代码

时间:2014-11-25 14:35:36

标签: ms-word word-vba

有没有办法创建VBA代码来通过音频读取word文档?好像计算机正在阅读文档上的内容。 (当然所有文字)。

2 个答案:

答案 0 :(得分:1)

我在google上找到了这个搜索。 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语音对象库

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

答案 1 :(得分:0)

如果您使用的是Windows 10,请小心 - 我有两个同名的库" Microsoft语音对象库" - 确保选择指向Windows / system32 / Speech / Common / sapi.dll

的那个