#include-once ; From Nine ;~ Opt("MustDeclareVars", True) #include Global Const $MSSPELLCHECKINGAPI_S_OK = 0 Global Const $MSSPELLCHECKINGAPI_S_FALSE = 1 Global Const $CLSID_SpellCheckerFactory = "{7AB36653-1796-484B-BDFA-E74F1DB7C1DC}" Global Const $IID_ISpellCheckerFactory = "{8E018A9D-2415-4677-BF08-794EA61F94BB}" Global Const $tag_ISpellCheckerFactory = _ "get_SupportedLanguages hresult(ptr*);" & _ "IsSupported hresult(wstr;bool*);" & _ "CreateSpellChecker hresult(wstr;ptr*);" Global Const $IID_ISpellChecker = "{B6FD0B71-E2BC-4653-8D05-F197E412770B}" Global Const $tag_ISpellChecker = _ "get_LanguageTag hresult(wstr*);" & _ "Check hresult(wstr;ptr*);" & _ "Suggest hresult(wstr;ptr*);" & _ "Add hresult(wstr);" & _ "Ignore hresult(wstr);" Global Const $IID_IEnumSpellingError = "{803E3BD4-2828-4410-8290-418D1D73C762}" Global Const $tag_IEnumSpellingError = _ "Next hresult(ptr*);" Global Const $IID_ISpellingError = "{B7C82D61-FBE8-4B47-9B27-6C0D2E0DE0A3}" Global Const $tag_ISpellingError = _ "get_StartIndex hresult(ulong*);" & _ "get_Length hresult(ulong*);" & _ "get_CorrectiveAction hresult(int*);" & _ "get_Replacement hresult(wstr*);" If Not @Compiled And StringRegExp(@ScriptName, "(?i)^MSSpellCheckingAPI(?:_\d{4}-\d{2}-\d{2}_\d{4})?\.au3$") Then _MSSpellCheckingAPI_Example() EndIf Func _MSSpellCheckingAPI_Example() ; Initialize Spell Checking API Local $oSpell = _MSSpellCheckingAPI_CreateSpell("en-US") ; English setup ;~ Local $oSpell = _MSSpellCheckingAPI_CreateSpell("fr-FR") ; French setup ;~ Local $oSpell = _MSSpellCheckingAPI_CreateSpell("pl-PL") ; Polish setup If @error Then Local $iInitError = @error, $iInitExtended = @extended MsgBox($MB_ICONERROR, "Spell Checking API", "Failed to initialize the spell checker." & @CRLF & _ "@error = " & $iInitError & @CRLF & "@extended = " & $iInitExtended) Return SetError($iInitError, $iInitExtended, '') EndIf ; Words to check Local $aWord = ["Spell Checking", "Speell", "Checcking"] ; English words ;~ Local $aWord = ["Test", "EntĂȘtĂ©", "Pasd'allure"] ; French words ;~ Local $aWord = ["Sprawdzanie pisowni", "sprawdanie", "pissowni"] ; Polish words Local $bResult = False For $sWord In $aWord $bResult = _MSSpellCheckingAPI_Check($oSpell, $sWord) If @error Then Local $iCheckError = @error, $iCheckExtended = @extended MsgBox($MB_ICONERROR, "Spell Checking API", "Spell checking failed for: " & $sWord & @CRLF & _ "@error = " & $iCheckError & @CRLF & "@extended = " & $iCheckExtended) ContinueLoop EndIf MsgBox(($bResult ? $MB_OK : $MB_ICONWARNING), ($bResult ? "Correct" : "Error"), $sWord) Next EndFunc ;==>_MSSpellCheckingAPI_Example Func _MSSpellCheckingAPI_CreateSpell($sLanguageTag = "en-US") ; Create the spell checker factory Local $oFactory = ObjCreateInterface($CLSID_SpellCheckerFactory, $IID_ISpellCheckerFactory, $tag_ISpellCheckerFactory) If Not IsObj($oFactory) Then If Not @Compiled Then ConsoleWrite("! Err : oFactory" & @CRLF) Return SetError(1, 0, 0) EndIf ; Verify that the requested language is supported Local $bSupported = False Local $iHRESULT = $oFactory.IsSupported($sLanguageTag, $bSupported) If $iHRESULT <> $MSSPELLCHECKINGAPI_S_OK Then If Not @Compiled Then ConsoleWrite("! Err : IsSupported HRESULT = " & $iHRESULT & @CRLF) Return SetError(2, $iHRESULT, 0) EndIf If Not $bSupported Then If Not @Compiled Then ConsoleWrite("! Err : Unsupported language tag: " & $sLanguageTag & @CRLF) Return SetError(3, 0, 0) EndIf ; Create the spell checker for the requested language Local $pSpellChecker = 0 $iHRESULT = $oFactory.CreateSpellChecker($sLanguageTag, $pSpellChecker) If $iHRESULT <> $MSSPELLCHECKINGAPI_S_OK Or $pSpellChecker = 0 Then If Not @Compiled Then ConsoleWrite("! Err : CreateSpellChecker HRESULT = " & $iHRESULT & @CRLF) Return SetError(4, $iHRESULT, 0) EndIf Local $oSpell = ObjCreateInterface($pSpellChecker, $IID_ISpellChecker, $tag_ISpellChecker) If Not IsObj($oSpell) Then If Not @Compiled Then ConsoleWrite("! Err : oSpell" & @CRLF) Return SetError(5, 0, 0) EndIf Return $oSpell EndFunc ;==>_MSSpellCheckingAPI_CreateSpell Func _MSSpellCheckingAPI_Check(ByRef $oSpell, $sWord) If Not IsObj($oSpell) Then Return SetError(1, 0, False) If $sWord = "" Then Return SetError(2, 0, False) ; Check the supplied text and obtain the spelling-error enumerator Local $pEnumSpellingError = 0 Local $iHRESULT = $oSpell.Check($sWord, $pEnumSpellingError) If $iHRESULT <> $MSSPELLCHECKINGAPI_S_OK Then If Not @Compiled Then ConsoleWrite("! Err : Check HRESULT = " & $iHRESULT & @CRLF) Return SetError(3, $iHRESULT, False) EndIf If $pEnumSpellingError = 0 Then If Not @Compiled Then ConsoleWrite("! Err : pEnumSpellingError" & @CRLF) Return SetError(4, 0, False) EndIf Local $oErrors = ObjCreateInterface($pEnumSpellingError, $IID_IEnumSpellingError, $tag_IEnumSpellingError) If Not IsObj($oErrors) Then If Not @Compiled Then ConsoleWrite("! Err : oErrors" & @CRLF) Return SetError(5, 0, False) EndIf ; S_OK means that at least one spelling error was returned. ; S_FALSE means that the enumerator contains no spelling errors. Local $pSpellingError = 0 $iHRESULT = $oErrors.Next($pSpellingError) Switch $iHRESULT Case $MSSPELLCHECKINGAPI_S_OK ; Wrap the returned interface pointer so AutoIt releases its COM reference. Local $oSpellingError = ObjCreateInterface($pSpellingError, $IID_ISpellingError, $tag_ISpellingError) If Not IsObj($oSpellingError) Then If Not @Compiled Then ConsoleWrite("! Err : oSpellingError" & @CRLF) Return SetError(6, 0, False) EndIf $oSpellingError = 0 Return False Case $MSSPELLCHECKINGAPI_S_FALSE Return True Case Else If Not @Compiled Then ConsoleWrite("! Err : IEnumSpellingError.Next HRESULT = " & $iHRESULT & @CRLF) Return SetError(7, $iHRESULT, False) EndSwitch EndFunc ;==>_MSSpellCheckingAPI_Check Func _MSSpellCheckingAPI_CheckWord($sWord) ; Reuse one Polish spell checker, but retry initialization after a previous failure. Local Static $oSpell = 0 If Not IsObj($oSpell) Then $oSpell = _MSSpellCheckingAPI_CreateSpell("pl-PL") If @error Then Return SetError(@error, @extended, False) EndIf Local $bResult = _MSSpellCheckingAPI_Check($oSpell, $sWord) Return SetError(@error, @extended, $bResult) EndFunc ;==>_MSSpellCheckingAPI_CheckWord