4

I want to recheck the spellings in a document after adding a dictionary. The problem is the following code (mostly from the macro recorder)

CustomDictionaries.Add FileName:="c:\test_dictionary.dic"

Application.ResetIgnoreAll
ActiveDocument.Range.SpellingChecked = False
ActiveDocument.Range.GrammarChecked = False

does not produce the same results as from the Word 2007's Word Options|Proofing|Recheck Document button. The button does recheck the document and you can see the newly added words get removed as misspellings. The code does not have any noticable affect.

What am I overlooking?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ForEachLoop
  • 2,508
  • 3
  • 18
  • 28

2 Answers2

2

I've had to do the same thing for a project, an this workaround made it happen for me:

 'spellcheck the document
 ActiveDocument.Range.LanguageID = wdFrenchHaiti
 ActiveDocument.Range.LanguageID = wdEnglishUS

When you change the language, Word rechecks the range for spelling errors.

1

This is the way Word works. Adding a CustomDictionary will not trigger it to be used right away, there are only a few way ways to trigger it. One of which is the Proofing dialog (i.e. you don't have to click "Recheck Document", you just need to click "OK" and it will recheck). Another trigger is to manually type in text and then a seperator (like a space or paragraph). Yet another trigger is the Spell Check dialog. Unfortunately, there don't seem to be any really good options I can see.

But here's a bad option, which I haven't tried. (Note: SendKeys doesn't work well on Vista/7, there is a replacement out there). After you've added your custom dictionary, bring up the proofing dialog and then programmatically click OK. Again, I haven't tried it really, so I'm not sure if this will produce the desired results.

Todd Main
  • 28,951
  • 11
  • 82
  • 146
  • @Otaku. Thanks. However, the CheckSpelling call displays the Spelling and Grammar dialog box. i just want to recheck the spelling within the document and have some words previously misspelled not to be. – ForEachLoop Aug 02 '10 at 16:22
  • @ForEachLoop: Gotcha. Have you tried to remove the line `ActiveDocument.CheckSpelling CustomDictionary:=dic`? That should just re-enable the red-squigglies without the dialog. – Todd Main Aug 02 '10 at 18:42
  • @Otaku. Test: Create c:\TestDictionary.dic with at least "asdf". Then run this from a doc: Public Sub CreateDictionaryTest() Dim currentDocument As Document Set currentDocument = ActiveDocument currentDocument.Range.InsertAfter "When in the kourse of asdf events." currentDocument.Range.InsertParagraphAfter Dim myDictionary As Dictionary Set myDictionary = CustomDictionaries.Add(FileName:="c:\TestDictionary.dic") ActiveDocument.SpellingChecked = False End Sub I'm assuming this code should have the same effect as the Recheck Document button. – ForEachLoop Aug 03 '10 at 20:25
  • @ForEachLoop: Oh, okay, I see what you're saying. See above for commentary. – Todd Main Aug 04 '10 at 21:16
  • @Otaku. I couldn't get any of those options to work. The call 'Dialogs(wdDialogToolsOptionsTabProofread).Show' pauses the script while it's up so you have to manually click in it. It doesn't look like the Spell Check dialog or the Proofing dialog by themselves forces a refresh, at least not in Word 2007. – ForEachLoop Aug 31 '10 at 15:04
  • @ForEachLoop: Yeah, I tried quite a bit on this as well a number of different options and couldn't get them to work either. It may be that there is no other way to do this than "the hard way", but closing and re-opening the document, or some other drastic routine. – Todd Main Sep 11 '10 at 05:36