Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Spell check without showing MSWord

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Spell check without showing MSWord

    I needed a spell checker, but without displaying MSWord.
    Here is the code, use it and change it as you wish...
    Code:
    '---------------------------------------------------------------
    'Author: Peter Redei
    'Date:   10/08/02
    'Subject:SpellChecker without displaying MSWord
    '---------------------------------------------------------------
    
    
    #COMPILE EXE
    #INCLUDE "oWord.inc"
    
    SUB Spell(sWord AS STRING, sSuggs() AS STRING)
        LOCAL oWordApp AS WordApplication
        DIM oSpellingSuggestions AS WordSpellingSuggestions ' Spelling Suggestions collection
        DIM oSpellingSuggestion AS WordSpellingSuggestion   ' Spelling Suggestion class
        DIM vBool       AS VARIANT
        DIM vVnt        AS VARIANT
        DIM vText       AS VARIANT
        DIM vRes        AS VARIANT
        DIM suggs       AS LONG
        DIM i           AS LONG
    
            SET oWordApp = WordApplication IN "Word.Application"
            IF ISFALSE ISOBJECT(oWordApp) THEN _
                SET oWordApp = NEW WordApplication IN "Word.Application"
            ' Could MSWORD be opened? If not we will not check the spelling
            IF ISTRUE ISOBJECT(oWordApp) THEN
    
                'add a document
                LET vVnt = 0
                OBJECT CALL oWordApp.Documents.Add TO vVnt
                LET vText = sWord
                LET vRes = 0
                OBJECT CALL oWordApp.GetSpellingSuggestions(vText) TO vRes
                SET oSpellingSuggestions = vRes
                LET vBool = 0
                OBJECT GET oSpellingSuggestions.Count TO vBool
                suggs = VARIANT#(vBool)
                IF suggs > 0 THEN
                    REDIM sSuggs(1 TO suggs)
                    FOR i = 1 TO suggs
                        LET vVnt = i
                        LET vRes = 0
                        OBJECT CALL oSpellingSuggestions.Item(vVnt) TO vRes
                        SET oSpellingSuggestion = vRes
                        LET vBool = 0
                        OBJECT GET oSpellingSuggestion.Name TO vBool
                        sSuggs(i) = VARIANT$(vBool)
                    NEXT
                END IF
                LET vVnt = 0
                OBJECT CALL oWordApp.ActiveWindow.Close(vVnt)
                OBJECT CALL oWordApp.Quit
                SET oSpellingSuggestion = NOTHING
                SET oSpellingSuggestions = NOTHING
            END IF
        SET oWordApp  = NOTHING
    END SUB
    FUNCTION PBMAIN()
        REDIM sSuggs(0) AS STRING
        Spell "Helo", sSuggs()
        IF UBOUND(sSuggs) = 0 THEN
            MSGBOX "The spelling seems to be correct"
        ELSE
            MSGBOX JOIN$(sSuggs(), $CRLF), 0, "How about one of these?"
        END IF
    END FUNCTION
    ------------------

    #2
    I've made a minor addition to your code, Peter, so that it doesn't close Word
    if Word is already in use when the spell check is run.

    Code:
    '---------------------------------------------------------------
    'Author: Peter Redei, with minor change by Simon Morgan
    'Date:   12/08/02
    'Subject: SpellChecker without displaying MSWord
    '---------------------------------------------------------------
    
    #COMPILE EXE
    #INCLUDE "oWord.inc"
    
    SUB Spell(sWord AS STRING, sSuggs() AS STRING)
        LOCAL oWordApp AS WordApplication, WordNotOpen AS LONG
        DIM oSpellingSuggestions AS WordSpellingSuggestions ' Spelling Suggestions collection
        DIM oSpellingSuggestion AS WordSpellingSuggestion   ' Spelling Suggestion class
        DIM vBool       AS VARIANT
        DIM vVnt        AS VARIANT
        DIM vText       AS VARIANT
        DIM vRes        AS VARIANT
        DIM suggs       AS LONG
        DIM i           AS LONG
            SET oWordApp = WordApplication IN "Word.Application"
            IF ISFALSE ISOBJECT(oWordApp) THEN
                SET oWordApp = NEW WordApplication IN "Word.Application"
                WordNotOpen=1  '%True              
            END IF
            ' Could MSWORD be opened? If not we will not check the spelling
            IF ISTRUE ISOBJECT(oWordApp) THEN
                'add a document
                LET vVnt = 0
                OBJECT CALL oWordApp.Documents.Add TO vVnt
                LET vText = sWord
                LET vRes = 0
                OBJECT CALL oWordApp.GetSpellingSuggestions(vText) TO vRes
                SET oSpellingSuggestions = vRes
                LET vBool = 0
                OBJECT GET oSpellingSuggestions.Count TO vBool
                suggs = VARIANT#(vBool)
                IF suggs > 0 THEN
                    REDIM sSuggs(1 TO suggs)
                    FOR i = 1 TO suggs
                        LET vVnt = i
                        LET vRes = 0
                        OBJECT CALL oSpellingSuggestions.Item(vVnt) TO vRes
                        SET oSpellingSuggestion = vRes
                        LET vBool = 0
                        OBJECT GET oSpellingSuggestion.Name TO vBool
                        sSuggs(i) = VARIANT$(vBool)
                    NEXT
                END IF
                LET vVnt = 0
                OBJECT CALL oWordApp.ActiveWindow.Close(vVnt)
                IF WordNotOpen THEN OBJECT CALL oWordApp.Quit
                SET oSpellingSuggestion = NOTHING
                SET oSpellingSuggestions = NOTHING
            END IF
        SET oWordApp  = NOTHING
    END SUB
    FUNCTION PBMAIN()
        REDIM sSuggs(0) AS STRING
        Spell "Helo", sSuggs()
        IF UBOUND(sSuggs) = 0 THEN
            MSGBOX "The spelling seems to be correct"
        ELSE
            MSGBOX JOIN$(sSuggs(), $CRLF), 0, "How about one of these?"
        END IF
    END FUNCTION

    Comment


      #3
      Your spell checker example is almost everything I need. Is there
      any sample code on how to add a word to the Microsoft Word
      dictionary.

      I can create the dialog needed to display the suggestion
      list, just need a way to add a word to the dictionary.

      Michael Vickery's sample is almost there but it does a lot of
      flashing when it brings up the Word spell check dialog
      unnecessarily when it is called repeatedly for all the words in
      a small paragraph entry textbox.

      The method I'm using is to parse out all the words in a paragraph
      and send them one at a time to the spell checker. I don't
      want anything to popup until I have a word that creates a
      suggestion list. At that time, I want the user to be allowed
      to add a word to the dictionary.
      Thanks,

      Bob Mechler

      ------------------

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎