2014-11-25 100 views
1

有没有什么办法可以创建VBA代码来通过音频读取文档?就好像电脑正在读取文件上的内容一样。 (当然是全部文字)。VBA代码读取文档文档

+0

这就是第三方软件[Dragon Natural lySpeaking](https://en.wikipedia.org/wiki/Dragon_NaturallySpeaking)。分享你的研究有助于每个人告诉我们你试过了什么,以及它为什么不符合你的需求。这表明你已经花时间去尝试帮助自己,它使我们避免重申明显的答案,最重要的是它可以帮助你得到更具体和相关的答案!另请参见[如何问](http://stackoverflow.com/help/how-to-ask) – 2014-11-25 14:39:51

+0

感谢Scott - 我还没有尝试过任何东西,因为我不知道VBA是否可以实现。谢谢 – GeekZinside 2014-11-25 14:55:59

回答

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 

代码要求微软语音对象库

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 
+0

谢谢,我会尝试这个并回复。 – GeekZinside 2014-11-25 17:29:50

+0

@幸运之后? – GlobalJim 2014-11-26 13:20:53

+0

对不起,延迟回复 - 它没有工作。我没有收到任何错误消息或任何音频。我会继续寻找并分享我的发现。 – GeekZinside 2015-01-07 15:20:35

0

要小心,如果你使用的是Windows 10 - 我有两个库具有相同的名称“微软语音对象库“ - 确保你选择了一个指向Windows/system32/Speech/Common/sapi.dll的文件。