跳轉到內容

Visual Basic for Applications/朗讀字串和文字

來自華夏公益教科書,面向開放世界的開放書籍

此頁面包含 Excel VBA 程式碼,用於朗讀字串的內容,即儲存在字串變數中的文字。 它可以適應在 MS Office 中的其它地方使用。

程式碼說明

[編輯 | 編輯原始碼]

將整個程式碼清單放入一個標準模組中,並將檔案儲存為帶有 xlsm 字尾的檔案。執行各種子例程以檢視程式碼的工作原理。

VBA 程式碼

[編輯 | 編輯原始碼]
Sub BasicExcelSpeech()
 'Speaks the supplied string text in a default Excel voice
 'Default voice is changed via Windows Control Panel
 
 'Named Parameters of Speak():
 'Text: the text to read (Required)
 'SpeakAsync:=0, waits until done, or with 1, code runs during play (Optional)
 'SpeakXML:=0 , normal setting, or with 1, to ignore xml tags (Optional)
 'Purge:=0 , normal play, or with 1, clears the present play (Optional)
  
 Application.Speech.Speak Text:="Hello", SpeakAsync:=0, SpeakXML:=0, Purge:=0
 
End Sub

Sub testSpeakEachDigit()
   
    SpeakEachDigit "0123456789"
    
End Sub

Function SpeakEachDigit(sIn As String) As Boolean
    'non API method
    'uses excel's speak function to read a string, chara by chara
    'one character at a time
    
    Dim n As Long, m As Long, sS As String
    
    Application.EnableSound = True
    For n = 1 To Len(sIn)
        DoEvents
        sS = Mid(sIn, n, 1) 'take one character
        Application.Speech.Speak sS, 0, 0, 0
    Next n
    
    SpeakEachDigit = True

End Function

Sub testSetupSpeechVoice()
    'Run this to test SetupSpeechVoice()
    
    Dim sTxt As String, nVoc As Integer, nSpd As Integer, nVol As Integer
    
    sTxt = "The quick brown fox jumps over the lazy dog 1234567890 times."
    nVoc = 1        'chosen voice 0 or 1
    nSpd = 0        'speed of reading -10 to +10
    nVol = 100      'volume level 0 to 100

    SetupSpeechVoice sTxt, nVoc, nSpd, nVol
    
End Sub

Function SetupSpeechVoice(sText As String, Optional ByVal nVoices As Integer, _
                          Optional ByVal nRate As Integer, _
                          Optional ByVal nLoudness As Integer) As Boolean
    'Selects voice using an index, rate of speech -10 to +10,
    'and volume 0-100 for Speech.Speak()
    'Needs a VBA editor reference to Microsoft Speech Object Library
            
    Dim voc As SpeechLib.SpVoice
    
    Set voc = New SpVoice
    
    'avoid wrong choice of voice
    If nVoices > voc.GetVoices.Count - 1 Or nVoices < 0 Then
        MsgBox "Voice integer is out of range"
        Exit Function
    End If
    
    With voc
        Set .Voice = .GetVoices.Item(nVoices)
        .Rate = nRate
        .Volume = nLoudness
        .Speak sText
    End With

    SetupSpeechVoice = True

End Function

Sub ListAvailableVoices()
    'run this to know the id of the available voices
    'Needs a VBA editor reference to Microsoft Speech Object Library
    
    Dim n As Integer, sAccum As String
    Dim voc As SpeechLib.SpVoice
    
    Set voc = New SpVoice
        For n = 0 To voc.GetVoices.Count - 1
            Set voc.Voice = voc.GetVoices.Item(n)
            sAccum = sAccum & " " & n & " - " & voc.Voice.GetDescription & vbCrLf
            voc.Speak "My voice index number is " & CStr(n)
        Next n
        MsgBox sAccum

End Sub
華夏公益教科書