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)
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:
I could get it work this way (I just don't like the code):
What is wrong with my first PB code?
Thanks,
Peter Redei
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
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
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
Thanks,
Peter Redei
Comment