Repository: nvaccess/nvda

Issue:
NVDA does not play the buzzer sound  in word when a speeling error is made
<!-- Please read the text in this edit field before filling it in.
Please thoroughly read NVDA's wiki article on how to fill in this template, including how to provide the required files.
Issues may be closed if the required information is not present.
https://github.com/nvaccess/nvda/wiki/Github-issue-template-explanation-and-examples
Please also note that the NVDA project has a Citizen and Contributor Code of Conduct which can be found at https://github.com/nvaccess/nvda/blob/master/CODE_OF_CONDUCT.md. NV Access expects that all contributors and other community members read and abide by the rules set out in this document while participating or contributing to this project. This includes creating or commenting on issues and pull requests. 
-->

### Steps to reproduce:
1. Open word
2. Enable "use UIA automation when accessing word documents" under NVDA settings/advanced
3. misspell a word and press space
4. NVDAS doesn't play the buzzer sound to indicate that you've made a mistake
### Actual behavior:
NVDA doesn't play the buzzer sound to indicate a spelling error
### Expected behavior:
NVDA will play the buzzer sound when a spelling error is made when it is enabled under document formatting settings
### System configuration
#### NVDA installed/portable/running from source:
Installed
#### NVDA version:
2020.4
#### Windows version:
windows 10 2004
#### Name and version of other software in use when reproducing the issue:
Words 365 latest version
#### Other information about your system:
I have the report spelling errors setting checked under the document settings diolog. Previously NVDA would play the sound but now it doesn't. I have the uia automation turned on under the advanced settings category as well
### Other questions
#### Does the issue still occur after restarting your computer?
yes
#### Have you tried any other versions of NVDA? If so, please report their behaviors.
no
#### If add-ons are disabled, is your problem still occurring?

#### Did you try to run the COM registry fixing tool in NVDA menu / tools?
no


Discussion:
Turning off UIA fixes the issue but then we lose the performance improvements it causes.
@michaelDCurran  According to the reporter of #12915 this issue still persists. It would be good to fix this if possible for 2021.3 since having indication of spelling errors as you type is a pretty useful feature and it no longer works for build of Office where UIA has been enabled by default.
I hope it can bee fixed in NVDA 2021.3.

If not, (or if there is another regression using UIA), there are two path:
* Revert the commit forcing UIA use for recent versions of Word
* Modify the option "Always use UI Automation to access Microsoft &Word document controls when available" to "use UI Automation to access Microsoft Word document controls" and a combobox with the following choices (as per Edge/Chromium):
  * Yes
  * No
  * Only if necessary
  * Default (only if necessary)

I have closed #12915, as it duplicates this report.  Re Check Spelling as you Write, with UI Automation not enabled in 2021.2 and previous, the buzz is still activated when an unrecognised word is written.  My preference is not to check spelling as I write due to Word sometimes stealthily changing a word I intended to use when Check Spelling is enabled.
Andrew


Resolving patch:
diff --git a/source/NVDAObjects/__init__.py b/source/NVDAObjects/__init__.py
--- a/source/NVDAObjects/__init__.py
+++ b/source/NVDAObjects/__init__.py
@@ -13,7 +13,7 @@
 import re
 import typing
 import weakref
-
+import core
 import textUtils
 from logHandler import log
 import review
@@ -1053,21 +1053,34 @@ def _reportErrorInPreviousWord(self):
 			# The one before that is the last of the word, which is what we want.
 			info.move(textInfos.UNIT_CHARACTER, -2)
 			info.expand(textInfos.UNIT_CHARACTER)
-			fields = info.getTextWithFields()
-		except RuntimeError:
-			return
-		except:
+		except Exception:
 			# Focus probably moved.
 			log.debugWarning("Error fetching last character of previous word", exc_info=True)
 			return
-		for command in fields:
-			if isinstance(command, textInfos.FieldCommand) and command.command == "formatChange" and command.field.get("invalid-spelling"):
-				break
-		else:
-			# No error.
-			return
-		import nvwave
-		nvwave.playWaveFile(os.path.join(globalVars.appDir, "waves", "textError.wav"))
+
+		# Fetch the formatting for the last word to see if it is marked as a spelling error,
+		# However perform the fetch and check in a future core cycle
+		# To give the content control more time to detect and mark the error itself.
+		# #12161: MS Word's UIA implementation certainly requires this delay.
+		def _delayedDetection():
+			try:
+				fields = info.getTextWithFields()
+			except RuntimeError:
+				log.debugWarning("Error fetching formatting for last character of previous word", exc_info=True)
+				return
+			for command in fields:
+				if (
+					isinstance(command, textInfos.FieldCommand)
+					and command.command == "formatChange"
+					and command.field.get("invalid-spelling")
+				):
+					break
+			else:
+				# No error.
+				return
+			import nvwave
+			nvwave.playWaveFile(os.path.join(globalVars.appDir, "waves", "textError.wav"))
+		core.callLater(50, _delayedDetection)
 
 	def _get_liveRegionPoliteness(self) -> aria.AriaLivePoliteness:
 		""" Retrieves the priority with which  updates to live regions should be treated.


Test patch: