问题
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?
回答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.
回答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.
来源:https://stackoverflow.com/questions/3389314/recheck-document-for-spellings-not-same-as-vba-code