Microsoft Word - Loop code for spell check mysteriously crashes at random

Asked By John Wirth on 05-Jul-12 07:22 AM
I created code which launches spell check, and of the user clicks close or cancel, and spelling errors are greater than 0, spell check instantly re-opens. The code works fine, however, I have noted that occasion when clicking close of cancel, the code crashes- it jumps ahead of the loop and spell check closes. I have no idea why. The section of code concerned is as follows:

Do If Dialogs(wdDialogToolsSpellingAndGrammar).Show = -1 Then Dialogs(wdDialogToolsSpellingAndGrammar).Show End If Loop Until ActiveDocument.SpellingErrors.Count = 0 And ActiveDocument.GrammaticalErrors.Count = 0


It's a literally a matter of clicking close say 9 times and the code loops, but on the 10th click the problem occurs. Nothing else is done, so I don't see why this happens.
wally eye replied to John Wirth on 05-Jul-12 12:30 PM
It might be a timing issue, try putting a DoEvents after the dialog:

Do
    If Dialogs(wdDialogToolsSpellingAndGrammar).Show = -1 Then
      Dialogs(wdDialogToolsSpellingAndGrammar).Show
    End If
    DoEvents
Loop Until ActiveDocument.SpellingErrors.Count = 0 _
    And ActiveDocument.GrammaticalErrors.Count = 0
John Wirth replied to wally eye on 05-Jul-12 06:40 PM
Thanks wally eye, I tried that but it made no difference. The odd thing is that it really does seem to be completely random in that nothing has chaged each time I click. If I use the code in isolation on my home PC which has Word 2010, the problem doesn't happen. At work, we're on 2003.