Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Word automation: can't enable red underlines for spelling check

My code needs to enable spelling check in MS Word 2010 so user can see red underlines under misspellings.

In current version I try to do this to enable spelling check:

wordApplication.Options.CheckSpellingAsYouType = true;
wordApplication.Options.SuggestSpellingCorrections = true;
wordApplication.ActiveDocument.ShowSpellingErrors = true;
wordApplication.ActiveDocument.CheckSpelling();

I open Word and type in "Test texxt string". After this I run my code but nothing happens: No underlines under texxt word

I don't see any underlines under "texxt". If I go to options I see that "check spelling as you type" option was enabled: enter image description here

Now if I check this property:

wordApplication.ActiveDocument.SpellingErrors[0].Text

It will contain "texxt" which means that spelling was actually checked.

Also if I simply restart MS Word and type in same text - red underlines become visible: enter image description here

I also tried to call Application.ScreenRefresh and set ScreenUpdating properties but this haven't given any effect.

Note: I run this code/macro in the beginning, when there is no text in a document. If some text is there - it works.

Note2: Exact steps to reproduce:

  1. Start MS Word. Go to options and disable "check spelling as you type". Restart Word.

  2. Start Word and run following macro:

    Options.CheckSpellingAsYouType = true
    Options.SuggestSpellingCorrections = true
    ActiveDocument.ShowSpellingErrors = true
    
  3. Type in following text: "Test texxt string.". Press Enter.

  4. Run following macro

    MsgBox ActiveDocument.SpellingErrors(1).Text

You will see "texxt" in MessageBox but not red underlines

like image 814
Oleg Avatar asked Oct 04 '22 18:10

Oleg


2 Answers

By accident found following solution: if after user typed in some text I call following code underlines are shown:

int iDummy = wordDocument.wordApp.ActiveDocument.SpellingErrors.Count;
like image 110
Oleg Avatar answered Oct 08 '22 02:10

Oleg


I can't leave a comment on Oleg's answer due to low rep, but it's worth noting here that, with Word 2013, using the Application.ActiveDocument.SpellingErrors.Count method seems to remove the current selection for some unfathomable reason, which could be problematic.

A hack to hide this effect by only checking the Count when no text is currently selected; e.g.:

// "document" is a Microsoft.Office.Tools.Word.Document
var selected = document.Application.Selection.Range;
if(Math.Abs(selected.End - selected.Start) == 0)
{
    var count = document.Application.ActiveDocument.SpellingErrors.Count;
}
like image 41
jeffmk Avatar answered Oct 08 '22 00:10

jeffmk