1. Home
  2. Computing & Technology
  3. Delphi Programming

Spell Checking from Delphi code using MS Word - Office Automation in Delphi

By Zarko Gajic, About.com

6 of 7

Finally: Delphi Source Code

Here goes the parse-and-spell-check procedure (to fully understand the code download the entire project):
procedure TForm1.btnSpellCheckClick (Sender: TObject) ;
var   colSpellErrors : ProofreadingErrors;
  colSuggestions : SpellingSuggestions;
  j : Integer;
  StopLoop : Boolean;
  itxtLen, itxtStart : Integer;
  varFalse : OleVariant;
begin
WordApp.Connect;
WordDoc.ConnectTo(WordApp.Documents.Add(EmptyParam, EmptyParam)) ;

//main loop
StopLoop:=False;
itxtStart:=0;
Memo.SelStart:=0;
itxtlen:=0;
while not StopLoop do begin
  {parse the memo text into words.}
  itxtStart := itxtLen + itxtStart;
  itxtLen := Pos(' ', Copy(Memo.Text,1+itxtStart, MaxInt)) ;
  if itxtLen = 0 then StopLoop := True;
  Memo.SelStart := itxtStart;
  Memo.SelLength := -1 + itxtLen;

  if Memo.SelText = '' then Continue;

  WordDoc.Range.Delete(EmptyParam,EmptyParam) ;
  WordDoc.Range.Set_Text(Memo.SelText) ;
  {call spell check}
  colSpellErrors := WordDoc.SpellingErrors;
  if colSpellErrors.Count <> 0 then begin
    colSuggestions := WordApp.GetSpellingSuggestions (colSpellErrors.Item(1).Get_Text) ;
    with frSpellCheck do begin
      edNID.text := colSpellErrors.Item(1).Get_Text;
      {fill in the list box with suggestions}
      lbSuggestions.Items.Clear;
      for j:= 1 to colSuggestions.Count do
        lbSuggestions.Items.Add(VarToStr(colSuggestions.Item(j))) ;
      lbSuggestions.ItemIndex := 0;
      lbSuggestionsClick(Sender) ;

      ShowModal;
      case frSpellCheck.ModalResult of
        mrAbort: Break;
        mrIgnore: Continue;
        mrOK:
          if sReplacedWord <> '' then begin
            Memo.SelText := sReplacedWord;
            itxtLen := Length(sReplacedWord) ;
          end;
      end;
    end;
  end;
end;

WordDoc.Disconnect;

varFalse:=False;
WordApp.Quit(varFalse) ;
Memo.SelStart := 0;
Memo.SelLength := 0;
end;
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Advanced Delphi Techniques
  5. OLE / COM / Automation
  6. Automation
  7. Spell Checking from Delphi code using MS Word

©2009 About.com, a part of The New York Times Company.

All rights reserved.