Repository: nvaccess/nvda

Issue:
Word 2010: need a way to query cursor position, i.e. how many inches from margin - very useful for formatting
**Reported by kevinchao89 on 2011-11-20 07:19**
Word 2010
winwordTextInproc-4800  

There’s no way to query for cursor location, which is useful when formatting document’s, especially when one is instructed or needs to
have certain bits of text  so many inches from margins (left, top,
etc.), e.g. text needs to be centered and 4 inches from bottom of
page.
**Blocking #5127**



Discussion:
Comment 1 by Ahiiron on 2011-11-27 19:52
Yes, I can think of a few situations where it would have been easier to format a complex document with NVDA, quickly gain margin info and the like. One can always get this through the menus/ribbons, but it's sort of inefficient and clunky.

Comment 2 by krish on 2013-04-17 06:29
I am able to produce margin info, and provide a shortcut key to this. 

Comment 3 by tstivers on 2014-12-21 18:04
Has this question been resolved anywhere?

Is this something that can be retrieved from the NVDA+Del command? CC @michaelDCurran @bhavyashah
@ehollig I have never practically used the NVDA+Delete key command. However, pressing it reveals how much percentage above, below, left or right of an edge of a screen the current text or navigator object is. This is helpful but I don't think it adequately addresses the original feature request of actually reporting how many inches from a particular margin something is. @Qchristensen Do you have any views on the amount of solvency of the existing NVDA+Del command with regards to this ticket?
The NVDA+Delete command does work as Bhavya describes in many cases, however, there are situations where it has been overwritten in order to provide more useful information, for instance, to give the sheet and cell location in Excel.  I think having it provide the location of the cursor is a useful use-case.
As a work-around I have the below macro in my normal document template for word. When I press Alt+Delete I get a message box with the information. Adding the relevant call(s) to the Word appModule should be easy.

```
Sub HorizontalPosition()
'
' HorizontalPosition Macro
'
    hLocation = Selection.Information( _
      wdHorizontalPositionRelativeToPage)
      vLocation = Selection.Information( _
      wdVerticalPositionRelativeToPage)
      hInches = Round(PointsToInches(hLocation), 2)
      vInches = Round(PointsToInches(vLocation), 2)
      Page = Selection.Information(wdActiveEndPageNumber)
    MsgBox hInches & " inches from left, " & vInches & " inches from top, page " & Page & ".", vbOKOnly, "Cursor Location"

End Sub
```
Hello: A kind call to @michaelDCurran and others: Is there any progress on this?
A user of a NVDA spanish mailing list is requesting this feature and provides these use cases:
* If the subject of certain documents is distributed into several lines, the margins need to be the same for each of these lines.
* It's also useful to check if a line signature is properly positioned respect the name.
* To insert a picture at the bottom of a page containing text, knowing its size, a procedure to check the current position it's needed too.
Thanks for addressing this.
The request on the spanish list can be found at
https://nvdaes.groups.io/g/lista/message/314
Cheers

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
@@ -89,6 +89,25 @@ def label(self):
 
 class WordDocumentTextInfo(UIATextInfo):
 
+	def _get_locationText(self):
+		rects=self._rangeObj.getBoundingRectangles()
+		left=rects[0]
+		top=rects[1]
+		# UIA has no good way yet to convert coordinates into user-configured distances such as inches or centimetres.
+		# Nor can it give us specific distances from the edge of a page.
+		# Therefore for now, get the screen coordinates, and if the word object model is available, use our legacy code to get the location text.
+		om=self.obj.WinwordWindowObject
+		if not om:
+			return super(WordDocumentTextInfo,self).locationText
+		try:
+			r=om.rangeFromPoint(left,top)
+		except (COMError,NameError):
+			log.debugWarning("MS Word object model does not support rangeFromPoint")
+			return super(WordDocumentTextInfo,self).locationText
+		from  NVDAObjects.window.winword import WordDocumentTextInfo as WordObjectModelTextInfo
+		i=WordObjectModelTextInfo(self.obj,None,_rangeObj=r)
+		return i.locationText
+
 	def _getTextWithFields_text(self,textRange,formatConfig,UIAFormatUnits=None):
 		if UIAFormatUnits is None and self.UIAFormatUnits:
 			# Word documents must always split by a unit the first time, as an entire text chunk can give valid annotation types 
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
@@ -65,6 +65,7 @@
 wdActiveEndPageNumber=3
 wdNumberOfPagesInDocument=4
 wdHorizontalPositionRelativeToPage=5
+wdVerticalPositionRelativeToPage=6
 wdFirstCharacterLineNumber=10
 wdWithInTable=12
 wdStartOfRangeRowNumber=13
@@ -562,6 +563,20 @@ def _get_unit_mouseChunk(self):
 			unit=textInfos.UNIT_SENTENCE
 		return unit
 
+	def _get_locationText(self):
+		textList=[]
+		offset=self._rangeObj.information(wdHorizontalPositionRelativeToPage)
+		distance=self.obj.getLocalizedMeasurementTextForPointSize(offset)
+		# Translators: a distance from the left edge of the page in Microsoft Word
+		textList.append(_("{distance} from left edge of page").format(distance=distance))
+		offset=self._rangeObj.information(wdVerticalPositionRelativeToPage)
+		distance=self.obj.getLocalizedMeasurementTextForPointSize(offset)
+		# Translators: a distance from the left edge of the page in Microsoft Word
+		textList.append(_("{distance} from top edge of page").format(distance=distance))
+		return ", ".join(textList)
+
+
+
 	def copyToClipboard(self):
 		self._rangeObj.copy()
 		return True


Test patch: