Can I use a character style to turn off spell checking for program code within a sentence in an MS Word document?

I asked this question in the Microsoft Office Forum, but then found the answer myself. That made it seem like a Superuser question!


As a Software Developer, I am frequently creating documents which include parts of computer programs. Naturally, computer programs are not written in English, so such code contains what appear to be spelling errors. When I include an entire section of code, I can create a "Code" style which not only formats using a fixed-width font like "Consolas", but it also turns off spell checking for the text with that style.

This doesn't help me when I include code in the middle of an English sentence. For instance:

  1. At this point in the program flow, the getSomethingForMe function is called.

I would really like to be able to create a style which would turn off spell checking just for the "code" text, and not for the entire document. Can I do so?


Here's what I tried:

  1. Create a new document
  2. Enter two lines of text, one spelled correctly, one with every word misspelled.
  3. Select the first word of the first line
  4. Click "Styles" in the pop-up toolbar
  5. Click "Create a style"
  6. Accept the defaults (Name="Style1"), but then click "Modify"
  7. Change the "Style type" to "Character"
  8. Click the "Format" drop down button at the bottom and choose "Language"
  9. (The "do not check spelling or grammar check box is filled-in-selected)
  10. Click the "Do not check spelling or grammar" checkbox, then click ok
  11. Click "Ok" to the "Create new style from formatting" dialog
  12. Select the first (misspelled) word of the second line
  13. Click "Styles" in the pop-up toolbar
  14. Click "Apply styles"
  15. Select "Style1" from the drop down
  16. Click "Reapply"
  17. Close the "Apply Styles" pop-up

The misspelled word is now in the "Style1" style, but is still flagged with a squiggly red line.

Clicking "Apply styles" again, and then "Modify", shows that the "Do not check spelling or grammar" checkbox is still filled-in-selected.

In other words, the language setting didn't "take".

This is what I remember that I used to be able to do with older versions of Word. For some reason, I can't get it to work now.


Solution 1:

Clicking "Apply styles" again, and then "Modify" shows that the "Do not check spelling or grammar" checkbox is still filled-in-selected.

In other words, the language setting didn't "take".

This is what I remember that I used to be able to do with older versions of Word. For some reason, I can't get it to work now.

OK, I figured it out on my own.

The quoted section was the clue. The "Do not check" setting was indeterminate. It's also the case that the selection of the language was indeterminate! The "English" language was not selected. Selecting "English" allowed the "Do not check" setting to "take".

Solution 2:

In the UI, you have to specify a human language to get the "Do not check" setting to stick. However, if you really want to have a character style with "do not check" that specifies no language, you can do it in the Open Office XML directly, or you can set up the style in VBA using

ActiveDocument.Styles("your style name").NoProofing = True

but you have to do it before you assign any language to the style.

On Windows, the behaviour changed sometime after Word 2000.

This was discussed in another "MS Answers" conversation (on the Mac side) here