Repository: nvaccess/nvda

Issue:
Reading by sentence is broken in MS Outlook and Word
In both the current and previous alpha snapshots, reading previous and next sentence with alt-up arrow and alt-down arrow respectively in both Outlook and Word 365 does not work.  This is regardless of whether Sentence Nav is installed.  With Input Help turned on, "Move to previous sentence" and "Move to next sentence" are reported.  However, pressing either combination out of help mode does nothing.
I am running the alphas as portable versions on Windows 10 April 2018 and tried with both eSpeak and Windows one core voices.

Andrew



Discussion:
I can confirm this for Word 2016, but only when the experimental UIA support is enabled. Could you please check the state of this setting in NVDA's advanced settings and report back your result?
Also, it would be greatly appreciated if you could update your initial description according to the issue template.

### Steps to reproduce:
Open either a document in Word or an email in Outlook
Press alt-up arrow to read previous sentence or alt-down arrow to read next sentence
### Actual behavior:
Nothing is presented by the screen reader
### Expected behavior:
Previous or next sentence respectively should be presented
### System configuration
#### NVDA installed/portable/running from source:
Portable
#### NVDA version:
nvda_snapshot_alpha-16691,10590c87,  nvda_snapshot_alpha-16732,03977ad4.exe and nvda_snapshot_alpha-16733,1a45aebb.
#### Windows version:
Windows 10 April 2018
#### Name and version of other software in use when reproducing the issue:
MS Word and Outlook - MS Office 365
#### Other information about your system:
The NVDA Use UI Automation to access Microsoft Word document controls when available checked
Problem does not occur if Use UI Automation to access Microsoft Word document controls when available is unchecked
### Other questions
#### Does the issue still occur after restarting your PC?
Yes
#### Have you tried any other versions of NVDA?
Yes, problem does not occur with 2018.4.1 and earlier
###Apologies
Slapped wrist severely for not following template.  As Leonard detected, the problem only occurs when UA Automation is used.  Log file does not seem to show anything relevant.

Andrew

@andrew-l-d: Can you still reproduce this?
Yes, I can still reproduce the issue with NVDA 2019.2.1.  When "Use UI Automation to access Microsoft Word document controls when available" is checked, reading by Sentence in MS Word and Outlook 365 still does not work.
Andrew

Using nvda_snapshot_alpha-23809,5f9750de navigating by sentence is not possible in MS Word or Outlook due to UI automation being the only option.  I have contacted the author of SentenceNav, whose current workload does not allow him to investigate for some time.  As I have argued in #8518, could navigating by sentence please be a core component of NVDA.  It is the only screen reader I am aware of that lacks the facility natively and it is an extremely important one.
I tried using Word's ctrl+shift+pageup/down to move by sentence.  NVDA does not present the new information.
UI Automation currently has no means of moving by sentence, though I believe this is planned via UI Automation Custom Patterns in MS Word. I have no timeline from Microsoft yet on when this will be available for NVDA to consume.
Until then, the question is whether this limitation is enough to hold back switching to UIA by default in MS Word for recent Office builds... waying that against the significant performance advantages of UIA.
Hi, I think an alternative would be documenting this limitation clearly so people can understand what they will experience until such time Office team inside Microsoft can implement necessary changes to UIA server-side implementation. Thanks. 


We may be able to allow a fallback to the object model for this specific feature if it is available, at least until UIA can expose this. I'll look into this.
I think we should make UIA in Word opt out when it is the default, e.g. have a similar combo box in settings as is used for UIA in Chromium.
If it is not known when MS will support sentence navigation in UIA documents, an alternative is to prioritize and implement #8518 in the meantime.

I strongly agree with Cyrille's comment, even without the UIA issue.  Reading by sentence should not be restricted to MS Office products.


Resolving patch:
diff --git a/source/NVDAObjects/UIA/wordDocument.py b/source/NVDAObjects/UIA/wordDocument.py
--- a/source/NVDAObjects/UIA/wordDocument.py
+++ b/source/NVDAObjects/UIA/wordDocument.py
@@ -5,6 +5,7 @@
 
 from comtypes import COMError
 from collections import defaultdict
+from scriptHandler import isScriptWaiting
 import textInfos
 import eventHandler
 import UIAHandler
@@ -12,12 +13,17 @@
 import controlTypes
 import ui
 import speech
+import review
+import braille
 import api
 import browseMode
 from UIABrowseMode import UIABrowseModeDocument, UIADocumentWithTableNavigation, UIATextAttributeQuicknavIterator, TextAttribUIATextInfoQuickNavItem
 from UIAUtils import *
 from . import UIA, UIATextInfo
-from NVDAObjects.window.winword import WordDocument as WordDocumentBase
+from NVDAObjects.window.winword import (
+	WordDocument as WordDocumentBase,
+	WordDocumentTextInfo as LegacyWordDocumentTextInfo
+)
 from scriptHandler import script
 
 
@@ -413,6 +419,46 @@ def event_UIA_notification(self, activityId=None, **kwargs):
 			return
 		super(WordDocument, self).event_UIA_notification(**kwargs)
 
+	# The following overide of the EditableText._caretMoveBySentenceHelper private method
+	# Falls back to the MS Word object model if available.
+	# This override should be removed as soon as UI Automation in MS Word has the ability to move by sentence.
+	def _caretMoveBySentenceHelper(self, gesture, direction):
+		if isScriptWaiting():
+			return
+		if not self.WinwordSelectionObject:
+			# Legacy object model not available.
+			# Translators: a message when navigating by sentence is unavailable in MS Word
+			ui.message(_("Navigating by sentence not supported in this document"))
+			gesture.send()
+			return
+		# Using the legacy object model,
+		# Move the caret to the next sentence in the requested direction.
+		legacyInfo = LegacyWordDocumentTextInfo(self, textInfos.POSITION_CARET)
+		legacyInfo.move(textInfos.UNIT_SENTENCE, direction)
+		# Save the start of the sentence for future use
+		legacyStart = legacyInfo.copy()
+		# With the legacy object model,
+		# Move the caret to the end of the new sentence.
+		legacyInfo.move(textInfos.UNIT_SENTENCE, 1)
+		legacyInfo.updateCaret()
+		# Fetch the caret position (end of the next sentence) with UI automation.
+		endInfo = self.makeTextInfo(textInfos.POSITION_CARET)
+		# Move the caret back to the start of the next sentence,
+		# where it should be left for the user.
+		legacyStart.updateCaret()
+		# Fetch the new caret position (start of the next sentence) with UI Automation.
+		startInfo = self.makeTextInfo(textInfos.POSITION_CARET)
+		# Make a UI automation text range spanning the entire next sentence.
+		info = startInfo.copy()
+		info.end = endInfo.end
+		# Speak the sentence moved to
+		speech.speakTextInfo(info, unit=textInfos.UNIT_SENTENCE, reason=controlTypes.OutputReason.CARET)
+		# Forget the word currently being typed as the user has moved the caret somewhere else.
+		speech.clearTypedWordBuffer()
+		# Alert review and braille the caret has moved to its new position
+		review.handleCaretMove(info)
+		braille.handler.handleCaretMove(self)
+
 	@script(
 		gesture="kb:NVDA+alt+c",
 		# Translators: a description for a script that reports the comment at the caret.


Test patch: