Repository: nvaccess/nvda

Issue:
Elements List not appearing or functioning with MS-Word 2010
When I have a document (attached) open in MS-Word 2010 and NVDA 2016.1 is running in browse mode I cannot make the Elements List appear or function.  When I hit INSERT+F7 the announcement for the elements list is made, but the dialog itself does not appear.  Since the document is full of hyperlinks, and if memory serves links should be the first element radio button, even if the dialog is not visible I should be able to tab into the tree view of the links, but hitting TAB simply jumps to the "Filter by" edit box.  Using left/right arrow when the elements dialog opens does absolutely nothing.

Just as a note, I am not experiencing the same issue in MS-Excel 2010.  When I hit INSERT+F7 the elements list immediately appears and I can interact with it.
[RR_Bentley_Resources.docx](https://github.com/nvaccess/nvda/files/222294/RR_Bentley_Resources.docx)



Discussion:
The tree view gets focus for me, but contains nothing.

Technical: We're getting this exception:

```
ERROR - unhandled exception (08:19:14):
Traceback (most recent call last):
  File "wx\_core.pyc", line 16766, in <lambda>
  File "browseMode.pyc", line 291, in run
  File "browseMode.pyc", line 635, in __init__
  File "browseMode.pyc", line 661, in initElementType
  File "NVDAObjects\window\winword.pyc", line 365, in iterate
  File "NVDAObjects\window\winword.pyc", line 288, in __init__
  File "NVDAObjects\window\winword.pyc", line 280, in rangeFromCollectionItem
  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))
```

It looks like `item.range` is throwing an error for some reason.

CC @MichaelDCurran.

James,

```
   Thanks very much for keeping me in the loop on this (and for looking
```

at it as quickly as you have)!

Brian Vogel

On Sun, Apr 17, 2016 at 6:22 PM, James Teh notifications@github.com wrote:

> The tree view gets focus for me, but contains nothing.
> 
> Technical: We're getting this exception:
> 
> ERROR - unhandled exception (08:19:14):
> Traceback (most recent call last):
>   File "wx_core.pyc", line 16766, in <lambda>
>   File "browseMode.pyc", line 291, in run
>   File "browseMode.pyc", line 635, in **init**
>   File "browseMode.pyc", line 661, in initElementType
>   File "NVDAObjects\window\winword.pyc", line 365, in iterate
>   File "NVDAObjects\window\winword.pyc", line 288, in **init**
>   File "NVDAObjects\window\winword.pyc", line 280, in rangeFromCollectionItem
>   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))
> 
> It looks like item.range is throwing an error for some reason.
> 
> CC @MichaelDCurran https://github.com/MichaelDCurran.
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly or view it on GitHub
> https://github.com/nvaccess/nvda/issues/5886#issuecomment-211124223

## 

_Bri the Tech Guy_          _http://britechguy.com http://britechguy.com_
brite britechguy@gmail.comchguy@gmail.com britechguy@gmail.com
  _(540) 324-5032_ or _(540) 849-0023                          *
"_If it's got you screaming, I'll help you stop!!*"

I was able to reproduce this with the attached document, I have found that there is one hyperlink in the document causing a problem. Interestingly it is just the left parenthesis character in the following text that is considered a hyperlink by word (tested with word 2013) :

> 341 Railroad Ave
> Bedford Hills, NY 10507-1404
> Phone:    (914) 242-8686 

If you try to edit the hyperlink properties, it seems as if it has no address or display text.

Looking at the docx source for the phone number line above has this:

``` xml
<w:r>
    <w:t xml:space="preserve">Phone:  </w:t>
</w:r>
<w:r><w:tab/></w:r>
<w:r w:rsidRPr="00B22258">
    <w:rPr>
        <w:vanish/>
    </w:rPr>
    <w:br/>
</w:r>
<w:hyperlink w:tgtFrame="_parent" w:history="1">
    <w:r w:rsidRPr="00B22258">
        <w:rPr>
            <w:rStyle w:val="Hyperlink"/>
            <w:vanish/>
        </w:rPr>
        <w:t>Get Directions</w:t>
    </w:r>
</w:hyperlink>
</w:p>
<w:p w:rsidR="00B22258" w:rsidRDefault="00B22258" w:rsidP="00DD2EAF">
    <w:r w:rsidRPr="00B22258">
        <w:t>(914) 242-8686</w:t>
    </w:r>
    <w:r>
        <w:t xml:space="preserve"> </w:t>
    </w:r>
</w:p>
```

It makes it look like this problematic hyperlink is in fact between the tab and left parenthesis characters. The hyperlink is not visible in the document.
Next I'll look to see if there is some way that we can detect something like this from NVDA, otherwise we may just have to catch the exception and move on. 


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
@@ -426,7 +426,15 @@ def iterate(self):
 			if self.direction=="previous":
 				index=itemCount-(index-1)
 			collectionItem=items[index]
-			item=self.quickNavItemClass(self.itemType,self.document,collectionItem)
+			try:
+				item=self.quickNavItemClass(self.itemType,self.document,collectionItem)
+			except COMError:
+				message = ("Error iterating over item with "
+					"type: {type}, iteration direction: {dir}, total item count: {count}, item at index: {index}"
+					"\nThis could be caused by an issue with some element within or a corruption of the word document."
+					).format(type=self.itemType, dir=self.direction, count=itemCount, index=index)
+				log.debugWarning(message ,exc_info=True)
+				continue
 			itemRange=item.rangeObj
 			# Skip over the item we're already on.
 			if not self.includeCurrent and isFirst and ((self.direction=="next" and itemRange.start<=self.rangeObj.start) or (self.direction=="previous" and itemRange.end>self.rangeObj.end)):


Test patch: