Spell Check Using Word

HMG Samples and Enhancements

Moderator: Rathinagiri

Post Reply
User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India

Spell Check Using Word

Post by bpd2000 »

Spell Check Using Word is possible, the condition is Word must be installed
Tested on winxp and word 2007 only
Pablo César may further upgrade / modify

Code: Select all

// bpd2000
// THIS EXAMPLE IS NEARLY FINISHED
//http://cuinl.tripod.com/Tips/off5.htm              // based on this
//http://www.contactez.net/support/vbscript.html
//https://www.foxite.com/archives/word-spell-check-0000139859.htm

#include "hmg.ch"

#xcommand TRY              => bError := errorBlock( {|oErr| break( oErr ) } ) ;;
                              BEGIN SEQUENCE
#xcommand CATCH [<!oErr!>] => errorBlock( bError ) ;;
                              RECOVER [USING <oErr>] <-oErr-> ;;
                              errorBlock( bError )
#xcommand FINALLY => ALWAYS

    STATIC oWord

    FUNCTION main()

    LOCAL cText := "Wouldd you be soo kind to showa us how to usea the word spelll checker with HMG. Thank's in advaance."

    MsgInfo(SpellCheck(cText),"Grammar Check")

    RETURN nil

    ***********

    STATIC FUNCTION SpellCheck(Texto)

    LOCAL oDoc,oTexto
    LOCAL cText:=Texto
    LOCAL lOldGramm
    LOCAL lOldUpper

    IF .NOT. IsWord()
       MsgInfo("MS Word is not installed in your computer.")
       RETURN .F.
    ENDIF


    oWord := CreateObject( "Word.Application" )

    oWord:Visible := .F.
    oWord:Documents:Add()
    
    oWord:Selection()
    oWord:Selection:TypeText(cText)
    
    lOldGramm := oWord:Options:CheckGrammarAsYouType()
    oWord:Options:CheckGrammarWithSpelling := .F.
    
    lOldUpper := oWord:Options:IgnoreUppercase()
    oWord:Options:IgnoreUppercase := .F.
        
    oWord:ActiveDocument:CheckSpelling()
    
    oWord:Selection:WholeStory()
    oWord:Selection:Copy()
    cText := oWord:Selection:Text()
    
    //‘Reset options and close everything
    oWord:Options:CheckGrammarAsYouType := lOldGramm
    oWord:Options:IgnoreUppercase := lOldUpper
    oWord:ActiveDocument:Close(0)
    oWord:Quit()
    oWord := Nil


    oTexto := NIL
    oDoc   := NIL
    oWord  := NIL

    IF ! EMPTY(cText) // If ctext is empty that means that the process was canceled
        Texto := cText
    ENDIF

    RETURN(Texto)

    *************

    FUNCTION isWord()

    local lSuccess:=.F.
    local oWord

    TRY
       oWord:= createObject("word.Application")
       lSuccess:=.T.
    CATCH
       lSuccess:=.F.
    END

    RETURN lSuccess

    ***************

Last edited by bpd2000 on Mon Apr 10, 2017 8:51 am, edited 1 time in total.
BPD
Convert Dream into Reality through HMG
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: Spell Check Using Word

Post by Pablo César »

Nice Dave !

I will revise it SAP :)
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
serge_girard
Posts: 3161
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Spell Check Using Word

Post by serge_girard »

Great !

Serge
There's nothing you can do that can't be done...
User avatar
serge_girard
Posts: 3161
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Spell Check Using Word

Post by serge_girard »

To get it working I had to change :
MsgInfo(SpellCheck(cText),“Grammar Check”) // use quotes or double quotes
AND
I had to have open Word on forehand.

Serge
There's nothing you can do that can't be done...
Post Reply