VbScript使计算机说出错误

时间:2014-10-30 02:35:01

标签: windows vbscript speech

我有这个让计算机说些什么的文件。我想让它循环使用VbCancel函数。我收到这个错误。代码到目前为止:

Do
    Dim Message, Speak
    Message=InputBox("Enter text","Speak")
    Set Speak=CreateObject("sapi.spvoice")
    MsgBox ("You entered: " & Speak)
    Speak.Speak Message
    If Len(Speak) = 0 Then
        MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
                       "If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
            If MyMessageBox = vbYes Then
                MsgBox "Operation Cancelled"
                Exit Sub
            End If
Loop
BTW错误是无效的退出声明
我正在使用Windows 7

2 个答案:

答案 0 :(得分:2)

Dim Message, Speak

Do
    Message=InputBox("Enter text","Speak")
    Set Speak=CreateObject("sapi.spvoice")
    MsgBox ("You entered: " & Message)
    Speak.Speak Message
    If Len(Message) = 0 Then
        MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
                       "If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
            If MyMessageBox = vbYes Then
                MsgBox "Operation Cancelled"
                Exit Do
            End If
    End If
Loop

这里有几个问题

  1. Exit Sub用于子程序。您试图退出Do循环
  2. Speak是一个对象。我不知道它是否有字符串属性,但它本身不是一个字符串。 Len(Speak)"You entered: " & Speak Speak都已更改为Message
  3. 您错过了End If
  4. 我将Dim语句移出循环。没有必要一遍又一遍地重新创建对象。

答案 1 :(得分:-2)

Dim Message, Speak

Do
    Message=InputBox("Enter text","Speak")
    Set Speak=CreateObject("sapi.spvoice")
    MsgBox ("You entered: " & Message)
    Speak.Speak Message
    If Len(Message) = 0 Then
        MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
                       "If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
            If MyMessageBox = vbYes Then
                MsgBox "Operation Cancelled"
                Exit Do
            End If
    End If
Loop