Announcement

Collapse
No announcement yet.

Spell check with PB 9.1

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

  • Spell check with PB 9.1

    I am trying to convert my previous code that worked with PB 7.0 (http://www.powerbasic.com/support/pb...light=spelling) to PB 9.1 using MSWord.

    Here is a test code for PB 9.1 (libWord1.inc was created by the PB COM Browser)
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "libWord1.inc"
    
    FUNCTION PBMAIN () AS LONG
         LOCAL oWordApp    AS Int__Application
         LOCAL oSpellingSuggestions AS Int_SpellingSuggestions
         LOCAL oSpellingSuggestion AS Int_SpellingSuggestion
         LOCAL i AS LONG
         LOCAL vSpellingSuggestion AS VARIANT
         LOCAL oDocuments AS Int_Documents
    
         oWordApp =  NEWCOM $PROGID_Word_Application
         IF ISTRUE ISOBJECT(oWordApp) THEN
             oWordApp.Documents.Add
             oDocuments = oWordApp.Documents
             'msgbox format$(oDocuments.Count)  'returns 1
             oSpellingSuggestion = oWordApp.GetSpellingSuggestions("Helo")  'crashes here when you leave the function
             IF ISTRUE ISOBJECT(oSpellingSuggestions) THEN
                FOR i = 1 TO  oSpellingSuggestions.Count
                    oSpellingSuggestion = oSpellingSuggestions.Item(i - 1)
                    MSGBOX oSpellingSuggestion.Name
                NEXT
                oWordApp.Documents.Close
            END IF
            oWordApp.Quit
         END IF               
    END FUNCTION
    This code is crashing at the line
    oSpellingSuggestions = oWordApp.GetSpellingSuggestions("Helo")

    The exact same code translated to VB6 works (using early binding). Here is the VB code:
    Code:
    Private Sub Command1_Click()
        Dim oSpellingSuggestions As SpellingSuggestions 'Collection
        Dim oSpellingSuggestion As SpellingSuggestion
        Dim oWord As Application
        
        Set oWord = New Application
        oWord.Documents.Add
        Set oSpellingSuggestions = oWord.GetSpellingSuggestions("helo")
        
        If oSpellingSuggestions.Count Then
            For Each oSpellingSuggestion In oSpellingSuggestions
                Debug.Print oSpellingSuggestion.Name
            Next
        End If
        Set oWord = Nothing
    End Sub
    I could get it work this way (I just don't like the code):
    Code:
    '---------------------------------------------------------------
    'Author: Peter Redei
    'Date:   08/23/09
    'Subject: SpellChecker without displaying MSWord for PB 9.1
    '---------------------------------------------------------------
    
    #COMPILE EXE
    #INCLUDE "libWord1.inc"
    
    SUB Spell ALIAS "Spell"(sWord AS STRING, sSuggs() AS STRING) EXPORT
        LOCAL oWordApp AS Int__Application, WordNotOpen AS LONG
        DIM dWordApp AS DISPATCH
        DIM oSpellingSuggestions AS DISPATCH    ' Spelling Suggestions collection
        DIM oSpellingSuggestion AS DISPATCH     ' 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 = Int__Application IN "Word.Application"
            IF ISFALSE ISOBJECT(oWordApp) THEN
                SET oWordApp = NEW Int__Application 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
                dWordApp = oWordApp
                'add a document
                LET vVnt = 0
                oWordApp.Documents.Add
                'OBJECT CALL oWordApp.Documents.Add TO vVnt
                LET vText = sWord
                LET vRes = 0
                OBJECT CALL dWordApp.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 dWordApp.ActiveWindow.Close(vVnt)
                IF WordNotOpen THEN OBJECT CALL dWordApp.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
    What is wrong with my first PB code?


    Thanks,


    Peter Redei
    Last edited by Peter Redei; 23 Aug 2009, 04:02 PM.

  • #2
    >What is wrong with my first PB code?

    Just off the top of my head, I think your problem is trying to use the "direct" syntax with the IDBIND (DISPATCH) interface.

    Then again, "crash" has many meanings.... depending on context.

    You can't mean a compile-time error, because you would have said so and included the error message.

    You can't mean an unexpected runtime error, since your code is not trapping either ERR or OBJRESULT.

    You can't mean a protection fault, since you would have said "GPF" and provided the error code.

    Darned, I'm out of ideas for what "crash" means in this context. You've stumped me.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Peter,

      I've only just started looking at the COM Browser. Can you tell me which registered library you used to create libword1.inc?

      Here is a test code for PB 9.1 (libWord1.inc was created by the PB COM Browser)
      I tried using the oWord.inc that was in the Samples directory but it gives me a duplicate definition error. So I'd like to create the same include as you used.

      Comment


      • #4
        A second question, please?

        How does the version of MSWord a user has on their system affect how your code will perform?

        In general, will it work with revisions other than the one I have on my PC? I might guess the answer is Yes, assuming that the word search interface of MSWord hasn't changed much in recent years.

        But, since I don't really know what the COM Browser does to create the include file, I won't be surprised either way.

        Comment

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