Jump to content
CarlD

Trap COM (SAPI) error?

Recommended Posts

Posted (edited)

Trying to write a script that lists the available TTS voices and speaks some sample text for each voice. What I have here works, except that I'm having trouble trapping the COM error that some voices trigger. After a few such errors, the script hangs. I confess that, for the error trapping, I'm just following the example scripts for the ObjEvent() function, recipe style, without fully understanding why it should (or should not) work here. Pointers will be gratefully accepted.

If Not $CmdLine[0] Then
    $sSay = "The quick brown fox jumped over the lazy dog."
Else
    $sSay = $CmdLine[1]
EndIf

Global $__g_oTemplateCOMErrorHandler = 0

$oVox = ObjCreate("SAPI.spVoice")
If @error Then Exit MsgBox(0, @ScriptName, "Error creating SAPI object")

$oVox.Volume = 100
$oVox.Rate = 0
$oVoices = $oVox.GetVoices()
For $vName in $oVoices
    $oVox.Voice = $vName
    ConsoleWrite($vName.GetDescription() & @CRLF)
    Template_COMErrorRegister()
    $oVox.Speak($vName.GetDescription())
    If @error Then
        ConsoleWrite("COM error" & @CRLF)
        Sleep(500)
        ContinueLoop
    EndIf
    $oVox.Speak($sSay)
    If @error Then
        ConsoleWrite("COM error" & @CRLF)
        Sleep(500)
        ContinueLoop
    EndIf
    Template_COMErrorUnregister()
Next
Exit

Func Template_COMErrorRegister()
    $__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc   ;==>Template_COMErrorRegister

Func Template_COMErrorUnregister()
    $__g_oTemplateCOMErrorHandler = 0
EndFunc   ;==>Template_COMErrorUnregister

Func Template_COMErrFunc()
    Sleep(500)
EndFunc   ;==>Template_COMErrFunc

 

Edited by CarlD
clarity

Share this post


Link to post
Share on other sites
Posted (edited)
4 hours ago, Danp2 said:

Try moving the register/unregister function calls outside your For / Next loop.

Works! (Of course.) Thank you very much. I will now have to figure out why.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ListVoices.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
If Not $CmdLine[0] Then
    $sSay = "The quick brown fox jumped over the lazy dog."
Else
    $sSay = $CmdLine[1]
EndIf

Global $__g_oTemplateCOMErrorHandler = 0, $iErrors = 0

$oVox = ObjCreate("SAPI.spVoice")
If @error Then Exit MsgBox(0, @ScriptName, "Error creating SAPI object")

$oVox.Volume = 100
$oVox.Rate = 0
$oVoices = $oVox.GetVoices()
Template_COMErrorRegister()
ConsoleWrite("Ctrl-C quits..." & @CRLF&@CRLF)
For $vName in $oVoices
    $i = 0
    $oVox.Voice = $vName
    ConsoleWrite($vName.GetDescription() & @CRLF)
    $oVox.Speak($vName.GetDescription() & @CRLF & $sSay)
    If COMError(@error) Then $iErrors += 1
Next
Template_COMErrorUnRegister()
Exit $iErrors

Func COMError($bErr)
    If $bErr Then
        ConsoleWrite("SAPI error" & @CRLF)
        Sleep(500)
    EndIf
    Return $bErr
EndFunc  ;==>TestForCOMError

Func Template_COMErrorRegister()
    $__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc   ;==>Template_COMErrorRegister

Func Template_COMErrorUnregister()
    $__g_oTemplateCOMErrorHandler = 0
EndFunc   ;==>Template_COMErrorUnregister

Func Template_COMErrFunc()
    Sleep(500)
EndFunc   ;==>Template_COMErrFunc

 

Edited by CarlD

Share this post


Link to post
Share on other sites

Did you try this:

or this:

 

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest beginning - communication with GitHub REST API Forum Rules *
Include Dependency Tree (Tool for analyzing script relations)
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) *

PDF Related:https://www.autoitscript.com/forum/topic/177368-how-to-get-reference-to-pdf-object-embeded-in-ie/ *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2020-11-20

Share this post


Link to post
Share on other sites
Posted (edited)
On 6/29/2020 at 6:57 AM, mLipok said:

Did you try this:

or this:

 

Thanks, I'll check these out. Meanwhile, my code now looks like this:

; ListVoices.au3 -- CLD rev. 2020-07-09
;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ListVoices.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Global $__g_oTemplateCOMErrorHandler = 0
Local $iErrors = 0
Local $sSay = "The quick brown fox jumped over the lazy dog."
Local $sVnm = "", $bShh = 0
If StringinStr($CmdLineRaw, "/q") Or StringinStr($CmdLineRaw, "-q") Then $bShh = 1

Switch $CmdLine[0]
    Case 0
        Sleep(10)
    Case 1
        If $CmdLine[1] = "/?" Or $CmdLine[1] = "-?" Then
            ConsoleWrite(@ScriptName & ": List|Sample text-to-speech voices" & @CRLF & "Usages:" & @CRLF & "1) List all installed voices and hear a test phrase:" & @CRLF & "   " & @ScriptName & " [""test phrase""]|/Q" & @CRLF & "   Default test phrase is:" & @CRLF & "     ""The quick brown fox jumped over the lazy dog.""" & @CRLF & "   /Q[uiet] = list only, no spoken output" & @CRLF & "2) List voice(s) matching|-excluding a voice name or description:" & @CRLF & "   " & @ScriptName & " /V [-][""]voice[""] [""test phrase""]|/Q" & @CRLF & "   [""]voice[""] may be a substring of the name or description; for example:" & @CRLF & "     " & @ScriptName & " /V George [/Q] ==> all voices named ""George""" & @CRLF & "     " & @ScriptName & " /V Canada [/Q] ==> all voices INcluding ""Canada""" & @CRLF & "     " & @ScriptName & " /V -English [/Q] ==> all voices EXcluding ""English""" & @CRLF & "3) View this help:" & @CRLF & "   " & @ScriptName & " /?" & @CRLF)
            Exit
        Else
            $sSay = $CmdLine[1]
        EndIf
    Case 2
        If $CmdLine[1] = "/v" Or $CmdLine[1] = "-v" Then
            $sVnm = $CmdLine[2]
        Else
            If $bShh Then
                $sSay = $CmdLine[1]
            Else
                $sSay = $CmdLine[1] & " " & $CmdLine[2]
            EndIf
        EndIf
    Case Else
        If $CmdLine[1] = "/v" Or $CmdLine[1] = "-v" Then
            $sVnm = $CmdLine[2]
            $sSay = $CmdLine[3]
        Else
            $sSay = $CmdLine[1] & " " & $CmdLine[2] & " " & $CmdLine[3]
            If $bShh Then
                $sSay = $CmdLine[1]
            Else
                $sSay = $CmdLine[1] & " " & $CmdLine[2] & " " & $CmdLine[3]
            EndIf
        EndIf
EndSwitch

$oVox = ObjCreate("SAPI.spVoice")
If @error Then Exit MsgBox(0, @ScriptName, "Error creating SAPI object")

$iCount = 0
$oVox.Volume = 100
$oVox.Rate = 0
$oVoices = $oVox.GetVoices()

Template_COMErrorRegister()
$bIn = 1
If $sVnm Then
    If StringInStr($sVnm, "-") = 1 Then
        $bIn = 0
        $sVnm = StringTrimLeft($sVnm, 1)
    EndIf
    For $vName in $oVoices
        $oVox.Voice = $vName
        If _StringInOrOutStr($vName.GetDescription(), $sVnm, $bIn) Then
            $iCount +=1
            $oVox.Voice = $vName
            ConsoleWrite($vName.GetDescription() & @CRLF)
            If Not $bShh Then $oVox.Speak($vName.GetDescription() & @CRLF & $sSay)
        EndIf
        If COMError(@error) Then $iErrors += 1
    Next
    If $iCount < 1 Then Consolewrite("No matching voice """ & $sVnm & """" & @CRLF)
    Exit
Else
    ConsoleWrite("Ctrl-C quits" & @CRLF & @ScriptName & " /? gives usage details" & @CRLF&@CRLF)
    For $vName in $oVoices
        $iCount += 1
        $oVox.Voice = $vName
        ConsoleWrite($iCount & ". " & $vName.GetDescription() & @CRLF)
        If Not $bShh Then $oVox.Speak($vName.GetDescription() & @CRLF & $sSay)
        If COMError(@error) Then $iErrors += 1
    Next
    Exit $iErrors
EndIf
Template_COMErrorUnRegister()

; Function definitions
; --------------------
Func _StringInOrOutStr($sString, $sSubString, $bIn = 1, $iCaseSense = 0, $iOccurrence = 1, $iStart = 1, $iCount = StringLen($sString))
#cs
------------------------------------------------------------------------
    Test for inclusion OR exclusion of substring in string
        If $bIn = 1 (test for inclusion), function and return value is
      same as StringInStr()
    If $bIn = 0 (test for exclusion), function returns 0 if
      StringInStr() > 0, else returns 1
------------------------------------------------------------------------
#ce
    $iReturn = StringInStr($sString, $sSubString, $iCaseSense, $iOccurrence, $iStart, $iCount)
    If Not $bIn Then
        If $iReturn Then
            $iReturn = 0
        Else
            $iReturn = 1
        EndIf
    EndIf
    Return $iReturn
EndFunc  ;==>_StringInOrOutStr

Func COMError($bErr)
    If $bErr Then
        ConsoleWrite("SAPI error" & @CRLF)
        Sleep(500)
    EndIf
    Return $bErr
EndFunc  ;==>TestForCOMError

Func Template_COMErrorRegister()
    $__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc   ;==>Template_COMErrorRegister

Func Template_COMErrorUnregister()
    $__g_oTemplateCOMErrorHandler = 0
EndFunc   ;==>Template_COMErrorUnregister

Func Template_COMErrFunc()
    Sleep(500)
EndFunc   ;==>Template_COMErrFunc
; --------------------

 

 

Edited by CarlD
Updated script

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...