Jump to content

AnyGui and Word Example


DW1
 Share

Recommended Posts

#NoTrayIcon
#include<Word.au3>
#include<ANYGUI.au3>
#include<array.au3>
#Include <GuiEdit.au3>
#include <GuiList.au3>
#Include <GuiCombo.au3>
#include <GuiConstants.au3>

;~ HotKeySet("!t", "_test")

$ver = RegRead("HKEY_CLASSES_ROOT\Word.Application", "")
If $ver <> "Microsoft Word Application"  Then
    MsgBox(0, "Error", "You must have word installed to use spell check in notepad")
    Exit
EndIf



_WordErrorHandlerRegister()
Global $oWordApp = _WordCreate("", 0, 0, 0)
Global $oDoc = $oWordApp.ActiveDocument
Global $oRange = $oDoc.Range
Global $oSpellCollection, $oAlternateWords
Global $aLangs[2][2] = [[1, ""], ["English", 1033]]


;~ Opt ("GUIOnEventMode", 1)
Run("notepad.exe")

WinWaitActive("")

$hWnd = _GuiTarget ("Untitled - Notepad")
_ArrayDisplay($hWnd)
$child = _TargetaddChild ("", 0, 0, 50000, 20, $hWnd)
$Button1 = GUICtrlCreateButton("Spell Check!", 0, 0, 100, 20)
$Button2 = GUICtrlCreateButton("Replace Word", 400, 0, 100, 20)
$ListBox1 = GUICtrlCreateList("", 100, 0, 150, 20, $LBS_NOTIFY + $WS_VSCROLL)
$ListBox2 = GUICtrlCreateList("", 250, 0, 150, 20, $LBS_NOTIFY + $WS_VSCROLL)
$edit = ControlGetHandle($hWnd, "", 15)
;~ MsgBox(0, "test", $edit )
;~ GUICtrlSetOnEvent($button2, "_SpellCheck")
$Combo = GUICtrlCreateCombo("", 500, 0, 80, 20)
For $i = 1 To $aLangs[0][0]
    _GUICtrlComboAddString($Combo, $aLangs[$i][0])
Next
GUICtrlSetState($Button2, $GUI_DISABLE)

GUISetState(@SW_SHOW)
_EndTarget ()


While WinExists($hWnd)
    $msg = GUIGetMsg()
    Switch $msg
;~      Case $GUI_EVENT_CLOSE, $Button3
;~          _Exit()
        Case $Button1
            If ControlGetText($hWnd, "", 15) <> "" Then
                _SpellCheck()
            Else
                MsgBox(0, "Error", "No text to check")
            EndIf
        Case $Button2
            _ReplaceWord()
        Case $ListBox1
            _SpellingSuggestions()
            _HighlightWord()
        Case $ListBox2
            GUICtrlSetState($Button2, $GUI_ENABLE)
        Case $Combo
            _SetLanguage()
            _SpellCheck()
    EndSwitch
    $size = WinGetClientSize($hWnd)
    If WinExists($hWnd) Then
        ControlMove($hWnd, "", 15, 0, 20, $size[0], $size[1] - 20)
        ControlMove($hWnd, "", "button2", $size[0], 25, 20, 20)
    EndIf
    Sleep(10)
WEnd



;~ While WinExists($hWnd)
;~  $size = WinGetClientSize($hWnd)
;~  If WinExists($hWnd) Then
;~      ControlMove($hWnd, "", 15, 0, 20, $size[0], $size[1] - 20)
;~      ControlMove($hWnd, "", "button2", $size[0], 25, 20, 20)
;~  EndIf
;~  Sleep(10)
;~ WEnd
_WordQuit($oWordApp, 0)

Func Eventer()
    MsgBox(0, "test", "hit")
EndFunc   ;==>Eventer


Func _test()
    MsgBox(0, "ControlGetText", ControlGetText($hWnd, "", 15))
    MsgBox(0, "GUICtrlRead", GUICtrlRead($edit))
EndFunc   ;==>_test






                    ; -----------------MS WORD FUNCS-----------------;
;----These funcs were written by big_daddy for his Custom Spell Checker----;


Func _SpellCheck()
    Local $sText, $sWord
    ; this will allow you to have the last word corrected
    ControlSetText($hWnd, "", 15, ControlGetText($hWnd, "", 15) & " ")
    ; TRY TO FIND A WAY AROUND THIS
    _GUICtrlListClear($ListBox1)
    _GUICtrlListClear($ListBox2)
    GUICtrlSetState($Button2, $GUI_DISABLE)

;~  $sText = GUICtrlRead($edit)
    $sText = ControlGetText($hWnd, "", 15)
    $oRange = $oWordApp.Activedocument.Range
    $oRange.Delete
    $oRange.InsertAfter ($sText)
    _SetLanguage()

    $oSpellCollection = $oRange.SpellingErrors
    If $oSpellCollection.Count > 0 Then
        For $i = 1 To $oSpellCollection.Count
            $sWord = $oSpellCollection.Item ($i).Text
            _GUICtrlListAddItem($ListBox1, $sWord)
        Next
    Else
        _GUICtrlListAddItem($ListBox1, "No spelling errors found.")
    EndIf
    ControlSetText($hWnd, "", 15, $oRange.Text)
;~  GUICtrlSetData($edit, $oRange.Text)
EndFunc   ;==>_SpellCheck

Func _SpellingSuggestions()
    Local $iWord, $sWord
    ;
    _GUICtrlListClear($ListBox2)
    GUICtrlSetState($Button2, $GUI_DISABLE)

    $iWord = _GUICtrlListSelectedIndex($ListBox1) + 1
    $sWord = $oSpellCollection.Item ($iWord).Text

    $oAlternateWords = $oWordApp.GetSpellingSuggestions ($sWord)
    If $oAlternateWords.Count > 0 Then
        For $i = 1 To $oAlternateWords.Count
            _GUICtrlListAddItem($ListBox2, $oAlternateWords.Item ($i).Name)
        Next
    Else
        _GUICtrlListAddItem($ListBox2, "No suggestions.")
    EndIf
EndFunc   ;==>_SpellingSuggestions

Func _HighlightWord()
    Local $sText, $iWord, $sWord, $iEnd, $iStart
    ;
    $iWord = _GUICtrlListSelectedIndex($ListBox1) + 1
    $sWord = $oSpellCollection.Item ($iWord).Text
    $sText = $oRange.Text

    $iStart = ($oSpellCollection.Item ($iWord).Start)
    $iEnd = ($oSpellCollection.Item ($iWord).End)
    _GUICtrlEditSetSel($edit, $iStart, $iEnd)
EndFunc   ;==>_HighlightWord

Func _ReplaceWord()
    Local $iWord, $iNewWord, $sWord, $sNewWord, $sText, $sNewText
    ;
    $iWord = _GUICtrlListSelectedIndex($ListBox1) + 1
    $iNewWord = _GUICtrlListSelectedIndex($ListBox2) + 1
    If $iWord == $LB_ERR Or $iNewWord == $LB_ERR Then
        MsgBox(48, "Error", "You must first select a word to replace, then a replacement word.")
        Return
    EndIf
    $oSpellCollection.Item ($iWord).Text = $oAlternateWords.Item ($iNewWord).Name

;~  GUICtrlSetData($edit, $oRange.Text)
    ControlSetText($hWnd, "", 15, $oRange.Text)

    _SpellCheck()


; TRYING THIS OUT

$string = ControlGetText( $hWnd, "", 15 )
While 1
    If StringRight($string, 1) = " "  Then
        $string = StringTrimRight($string, 1)
    Else
        ExitLoop
    EndIf
WEnd
ControlSetText( $hWnd, "", 15, $string)


;TESTTESTTEST


    GUICtrlSetState($Button2, $GUI_DISABLE)
EndFunc   ;==>_ReplaceWord

Func _SetLanguage()
    $sLang = GUICtrlRead($Combo)
    If $sLang <> "" Then
        $oWordApp.CheckLanguage = False
        For $i = 1 To $aLangs[0][0]
            If $sLang = $aLangs[$i][0] Then
                $WdLangID = Number($aLangs[$i][1])
            EndIf
        Next

        If $WdLangID Then
            With $oRange
                .LanguageID = $WdLangID
                .NoProofing = False
            EndWith
        EndIf
    Else
        $oWordApp.CheckLanguage = True
    EndIf
EndFunc   ;==>_SetLanguage

Func _Exit()
    _WordQuit($oWordApp, 0)
    Exit
EndFunc   ;==>_Exit

Edited by danwilli
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...