Repository: nvaccess/nvda

Issue:
Unable to read tables in Outlook 2016
NVDA logs the following error while trying to read tables in Outlook 2016 (as part of Office 365). Blank lines in the table are announced as blank, but it seems this error is triggered when there is anything in a cell.

ERROR - scriptHandler.executeScript (12:55:40):
error executing script: <bound method MailViewerTreeInterceptor.script_moveByLine_forward of <appModules.outlook.MailViewerTreeInterceptor object at 0x0518D210>> with gesture u'down arrow'
Traceback (most recent call last):
  File "scriptHandler.pyc", line 187, in executeScript
  File "cursorManager.pyc", line 202, in script_moveByLine_forward
  File "cursorManager.pyc", line 134, in _caretMovementScriptHelper
  File "speech.pyc", line 808, in speakTextInfo
  File "browseMode.pyc", line 992, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1131, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 711, in get
  File "NVDAObjects\window\winword.pyc", line 1209, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1146, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))



Discussion:
I'm seeing a similar issue with Outlook and tables. Essentially, NVDA throws error sound when an email with tables is read with Read all command. Here is a log snippet that I've isolated. I'm also attaching a full log for reference. 
NVDA version next-14139,11020922
Windows version 10 1703 build 16226 
 

[NVDA Outlook tables.txt](https://github.com/nvaccess/nvda/files/1107543/NVDA.Outlook.tables.txt)
[NVDA Outlook tables.txt](https://github.com/nvaccess/nvda/files/1107544/NVDA.Outlook.tables.txt)
[NVDA Outlook tables.txt](https://github.com/nvaccess/nvda/files/1107545/NVDA.Outlook.tables.txt)
INFO - _UIAHandler.UIAHandler.MTAThreadFunc (13:46:34.167):
UIAutomation: IUIAutomation3
INFO - core.main (13:46:35.279):
NVDA initialized
ERROR - eventHandler.executeEvent (16:46:38.930):
error executing event: gainFocus on <NVDAObjects.Dynamic_OutlookWordDocumentIAccessibleWordDocumentWindowNVDAObject object at 0x07261E10> with extra args of {}
Traceback (most recent call last):
  File "eventHandler.pyc", line 138, in executeEvent
  File "eventHandler.pyc", line 171, in doPreGainFocus
  File "browseMode.pyc", line 1109, in event_treeInterceptor_gainFocus
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - scriptHandler.executeScript (16:46:40.453):
error executing script: <bound method MailViewerTreeInterceptor.script_moveByLine_back of <appModules.outlook.MailViewerTreeInterceptor object at 0x0728EC10>> with gesture u'up arrow'
Traceback (most recent call last):
  File "scriptHandler.pyc", line 187, in executeScript
  File "cursorManager.pyc", line 205, in script_moveByLine_back
  File "cursorManager.pyc", line 141, in _caretMovementScriptHelper
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - eventHandler.executeEvent (16:46:42.561):
error executing event: gainFocus on <NVDAObjects.Dynamic_OutlookWordDocumentIAccessibleWordDocumentWindowNVDAObject object at 0x072788F0> with extra args of {}
Traceback (most recent call last):
  File "eventHandler.pyc", line 138, in executeEvent
  File "eventHandler.pyc", line 171, in doPreGainFocus
  File "browseMode.pyc", line 1109, in event_treeInterceptor_gainFocus
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - scriptHandler.executeScript (16:46:43.907):
error executing script: <bound method MailViewerTreeInterceptor.script_moveByLine_back of <appModules.outlook.MailViewerTreeInterceptor object at 0x0728EC10>> with gesture u'up arrow'
Traceback (most recent call last):
  File "scriptHandler.pyc", line 187, in executeScript
  File "cursorManager.pyc", line 205, in script_moveByLine_back
  File "cursorManager.pyc", line 141, in _caretMovementScriptHelper
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - scriptHandler.executeScript (16:46:44.838):
error executing script: <bound method MailViewerTreeInterceptor.script_moveByCharacter_forward of <appModules.outlook.MailViewerTreeInterceptor object at 0x0728EC10>> with gesture u'right arrow'
Traceback (most recent call last):
  File "scriptHandler.pyc", line 187, in executeScript
  File "cursorManager.pyc", line 196, in script_moveByCharacter_forward
  File "cursorManager.pyc", line 141, in _caretMovementScriptHelper
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - scriptHandler.executeScript (16:46:45.437):
error executing script: <bound method MailViewerTreeInterceptor.script_moveByCharacter_forward of <appModules.outlook.MailViewerTreeInterceptor object at 0x0728EC10>> with gesture u'right arrow'
Traceback (most recent call last):
  File "scriptHandler.pyc", line 187, in executeScript
  File "cursorManager.pyc", line 196, in script_moveByCharacter_forward
  File "cursorManager.pyc", line 141, in _caretMovementScriptHelper
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - eventHandler.executeEvent (16:46:52.782):
error executing event: gainFocus on <NVDAObjects.Dynamic_OutlookWordDocumentIAccessibleWordDocumentWindowNVDAObject object at 0x07278410> with extra args of {}
Traceback (most recent call last):
  File "eventHandler.pyc", line 138, in executeEvent
  File "eventHandler.pyc", line 171, in doPreGainFocus
  File "browseMode.pyc", line 1109, in event_treeInterceptor_gainFocus
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - eventHandler.executeEvent (16:47:33.668):
error executing event: gainFocus on <NVDAObjects.Dynamic_OutlookWordDocumentIAccessibleWordDocumentWindowNVDAObject object at 0x05519450> with extra args of {}
Traceback (most recent call last):
  File "eventHandler.pyc", line 138, in executeEvent
  File "eventHandler.pyc", line 171, in doPreGainFocus
  File "browseMode.pyc", line 1109, in event_treeInterceptor_gainFocus
  File "speech.pyc", line 828, in speakTextInfo
  File "browseMode.pyc", line 1001, in getControlFieldSpeech
  File "textInfos\__init__.pyc", line 434, in getControlFieldSpeech
  File "speech.pyc", line 1166, in getControlFieldSpeech
  File "NVDAObjects\window\winword.pyc", line 726, in get
  File "NVDAObjects\window\winword.pyc", line 1230, in fetchAssociatedHeaderCellText
  File "NVDAObjects\window\winword.pyc", line 1167, in getHeaderCellTrackerForTable
  File "comtypesMonkeyPatches.pyc", line 35, in new__getattr__
  File "comtypes\client\lazybind.pyc", line 149, in __getattr__
  File "comtypes\automation.pyc", line 664, in _invoke
COMError: (-2146822463, None, (u'Object has been deleted.', u'Microsoft Word', u'wdmain11.chm', 25305, None))
ERROR - RPC process 10780 (nvda_slave.exe) (11:02:33.309):
__main__.main:
slave error
Traceback (most recent call last):
  File "nvda_slave.pyw", line 90, in main
  File "comHelper.pyc", line 22, in _lresultFromGetActiveObject
  File "comtypes\client\__init__.pyc", line 180, in GetActiveObject
  File "comtypes\__init__.pyc", line 1165, in GetActiveObject
  File "_ctypes/callproc.c", line 950, in GetResult
WindowsError: [Error -2147221021] Operation unavailable
ERROR - RPC process 7208 (nvda_slave.exe) (11:03:00.365):
__main__.main:
slave error
Traceback (most recent call last):
  File "nvda_slave.pyw", line 90, in main
  File "comHelper.pyc", line 22, in _lresultFromGetActiveObject
  File "comtypes\client\__init__.pyc", line 180, in GetActiveObject
  File "comtypes\__init__.pyc", line 1165, in GetActiveObject
  File "_ctypes/callproc.c", line 950, in GetResult
WindowsError: [Error -2147221021] Operation unavailable

Apologies. For some reason, Github didn't want to take my file attachment for the full log.  
[NVDA Outlook tables.txt](https://github.com/nvaccess/nvda/files/1107547/NVDA.Outlook.tables.txt)


Resolving patch:
diff --git a/source/NVDAObjects/window/winword.py b/source/NVDAObjects/window/winword.py
--- a/source/NVDAObjects/window/winword.py
+++ b/source/NVDAObjects/window/winword.py
@@ -1173,7 +1173,13 @@ def populateHeaderCellTrackerFromBookmarks(self,headerCellTracker,bookmarks):
 	_curHeaderCellTracker=None
 	def getHeaderCellTrackerForTable(self,table):
 		tableRange=table.range
-		if not self._curHeaderCellTrackerTable or not tableRange.isEqual(self._curHeaderCellTrackerTable.range):
+		# Sometimes there is a valid reference in _curHeaderCellTrackerTable,
+		# but we get a COMError when accessing the range (#6827)
+		try:
+			tableRangesEqual=tableRange.isEqual(self._curHeaderCellTrackerTable.range)
+		except (COMError, AttributeError):
+			tableRangesEqual=False
+		if not tableRangesEqual:
 			self._curHeaderCellTracker=HeaderCellTracker()
 			self.populateHeaderCellTrackerFromBookmarks(self._curHeaderCellTracker,tableRange.bookmarks)
 			self.populateHeaderCellTrackerFromHeaderRows(self._curHeaderCellTracker,table)


Test patch: