5

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

8
  • Is this VBA? VBA doesn't use semicolons at the end of the lines, and the last line wouldn't include the parentheses. When I make those changes and run those lines of code, texxt is underlined and a Spelling dialog box pops up asking me about changing it. When you run your code you get no errors?
    – Christina
    Jun 25, 2013 at 15:42
  • That is C#. But I have same problems with VBA. It works only if document contains some text but if it is empty and this macros is run on the beginning when I start typing soemthing - I don't see any red underlines.
    – Oleg
    Jun 26, 2013 at 6:35
  • I don't want to post this as an answer because I can't really reproduce what you're getting. However, I did find that after running your macro Word would still autocorrect simple errors. I added the line Application.AutoCorrect.ReplaceTextFromSpellingChecker = False which caused the red lines to appear.
    – Christina
    Jun 26, 2013 at 22:14
  • You should find after your code has run that the 'as you type' check will not happen until you make a change to the document. You may also need to wait as it is a background job. It is not related to the CheckSpellings call so will not affect the display. Jun 28, 2013 at 10:28
  • @Christina, manipulating with AutoCorrect.ReplaceTextFromSpellingChecker haven't helped me
    – Oleg
    Jul 2, 2013 at 8:09

2 Answers 2

3

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;
1
  • Problem with this solution is that you need to execute this code each time after new text is added.
    – Oleg
    Jul 5, 2013 at 15:04
1

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;
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.