Mega Code Archive

 
Categories / Delphi / Activex OLE
 

Using MS Word Spell Checker and Suggestion List

Title: Using MS Word Spell Checker and Suggestion List Question: .. Create (procedure) .. Free (procedure) .. Active (property) .. CheckSpelling (function) This serves as an example of using MS Word to check the spelling of a word and to give a list of suggested correct spellings. I am using it as part of a VCL that will check a string of Text word by word and selectively replace any mispelt words. (For the full VCL see my atricle 1712 "VCL MS Word Spell Check and Thesaurus" under the VCL General category.) In the supplied example the text of an edit box and the items property of a list box is passed to the class function CheckSpelling(). This function returns true if the word is not mispelt. If the return value is false then the list of suggested spelling are returned in the listbox. Answer: unit Unit1; interface uses Windows, Sysutils, Forms, Buttons, StdCtrls, Classes, Dialogs, ComObj, Controls; type TForm1 = class(TForm) ListBox1: TListBox; Edit1: TEdit; Button1: TButton; Label1: TLabel; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormShow(Sender: TObject); procedure Button1Click(Sender: TObject); private public end; // Actual Spell Checker Class TSpellCheck = class(TObject) private MsWordApp, MsSuggestions : OleVariant; FActive : boolean; public constructor Create; destructor Destroy; override; function CheckSpelling(StrWord : string; Suggestions : TStrings) : boolean; property Active : boolean read FActive; end; var Form1: TForm1; implementation {$R *.DFM} // Spell Checker Methods -------------------------------------------- constructor TSpellCheck.Create; begin try MsWordApp := CreateOleObject('Word.Application'); FActive := true; MsWordApp.Documents.Add; except on E: Exception do begin MessageDlg('Cannot Connect to MS Word',mtError,[mbOk],0); FActive := false; end; end; end; destructor TSpellCheck.Destroy; begin if FActive then begin MsWordApp.Quit; MsWordApp := VarNull; end; inherited Destroy; end; function TSpellCheck.CheckSpelling(StrWord : string; Suggestions : TStrings) : boolean; var Retvar : boolean; i : integer; begin RetVar := false; Suggestions.Clear; if FActive then begin if MsWordApp.CheckSpelling(StrWord) then RetVar := true else begin MsSuggestions := MsWordApp.GetSpellingSuggestions(StrWord); for i := 1 to MsSuggestions.Count do Suggestions.Add(MsSuggestions.Item(i)); MsSuggestions := VarNull; end; end; Result := RetVar; end; // ----------------------------------------------------------------- // SIMPLE Example. var SPcheck : TSpellCheck; procedure TForm1.FormShow(Sender: TObject); begin SPcheck := TSpellCheck.Create; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin SPcheck.Free; end; procedure TForm1.Button1Click(Sender: TObject); begin if SPcheck.CheckSpelling(Edit1.Text,ListBox1.Items) then Label1.Caption := 'Word OK' else Label1.Caption := 'Mispelling'; end; end