As a general rule, it's good practice to explicitly declare variables. In your code below, for instance, the variable speech is declared without a data type, which means that it is a variant. In most cases, this works, but, in some cases, maybe not, or at least, not so efficiently.
Next, try explicit declaration and instantiation of the speech objects.
i.e.:
Code:
Dim ObjTextToSpeech as SAPI.spVoice
Set ObjTextToSpeech = new SAPI.spVoice
...
' when done,
set ObjTextToSpeech =nothing
Ran a quick test using MS' speech library.
Created a form with a textbox, two command buttons, a short picturebox anchored to the bottom containing a label. The controls were names (respectively) txtText, cmdTalk, cmdSing, picStat, and lblStat.
Ref the attached image, as well as the code below.
Code:
Option Explicit
Dim sSpeech As String
Dim oSpeech As SpeechLib.SpVoice
Private Sub cmdSing_Click()
Const sLyric As String = "~N~ bottles of beer on the wall! ~N~ bottles of beer! Take 1 down, pass it around, ~N-1~ bottles of Beer on the wall..."
Dim N As Integer
For N = 99 To 1 Step -1
sSpeech = Replace(sLyric, "~N~", CStr(N))
If N = 1 Then
sSpeech = Replace(sSpeech, "~N-1~", "NO") & " And, we're all done!"
Else
sSpeech = Replace(sSpeech, "~N-1~", CStr(N - 1))
End If
Me.lblStat.Caption = CStr(N - 1) & " Bottles: " & CStr(oSpeech.Speak(sSpeech))
DoEvents
Next N
Set oSpeech = Nothing
End Sub
Private Sub cmdTalk_Click()
sSpeech = Me.txtText.Text
Me.lblStat.Caption = CStr(oSpeech.Speak(sSpeech))
Set oSpeech = Nothing
End Sub
Private Sub Form_Load()
' instantiate the voice
Set oSpeech = New SpeechLib.SpVoice
End Sub