Repository: nvaccess/nvda

Issue:
Report background and highlighting colors in Microsoft Word
I noticed that NVDA is unable to report background and highlighting color changes in Microsoft Word. Check [the attachment](https://github.com/nvaccess/nvda/files/208986/Color.reporting.zip) for an example.

Steps to reproduce
1. Type a two lines of text in an empty document
2. Select the second line of text
3. Press alt+h,i and select a highlighting color
4. Select again, press alt+h,h and select a differenc background color
5. Arrow up and down in the document with color reporting enabled

Actual result: NVDA reports no difference
Proposed result: NVDA reports both background and highlighting color changes

NVDA cannot read MS Word highlighting without checking "Use UI Automation to access Microsoft Word document controls.

### Steps to reproduce:

1. Download the attached test.zip and unzip it.
2. Open the unzipped test.docx
3. Make sure "Use UI Automation to access Microsoft Word document controls" is unchecked in the NVDA advanced settings
4. Ensure that "Colors" is checked in the NVDA document formatting settings
5. Use the arrow keys to navigate through this document.
6. Check "Use UI Automation to access Microsoft Word document controls when available" in the NVDA advanced settings.
7. Use the arrow keys to navigate through this document.

### Actual behavior:
1. Execute step (5) NVDA will read out "I am a student."
2. Execute step (7) NVDA will read out "I bright yellow background am white background a grey background student white background."

### Expected behavior:
Both step (5) and step (7) should be read aloud as """I bright yellow background am white background a grey background student white background."

### System configuration
#### NVDA installed/portable/running from source:
installed andportable
#### NVDA version:
NVDA2020.3 andNVDA2020.4
#### Windows version:
Windows 10 20H2 (64-bit) build 19042.844

#### Name and version of other software in use when reproducing the issue:
Microsoft Word for Microsoft 365 MSO (16.0.13628.20318) 64-bit 

#### Other information about your system:
none

### 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 addons are disabled, is your problem still occuring?
yes

#### Did you try to run the COM registry fixing tool in NVDA menu / tools?
yes



Discussion:
Given the recent work that has been done to improve MS Word support (#2961, #5800, #5946), I'd like to bring this ticket under attention again.

@feerrenrut, mind assigning a priority to this?
Requested the enhancement in Japan, regarding Microsoft Outlook as well.
This would be really helpful. I hope it will come any time in the future into NVDA.
Hi @leonardder ,

I heavily played with UIA and VBA in recent days, around this situation.

Imho, it's very confused even API side.

Had you find a reliable way to distinguish between background and highlight color?

I found only [this property](https://learn.microsoft.com/en-us/office/vba/api/Word.Range.HighlightColorIndex) useful to be sure whether there is an highlighting and what's the color applied...
I haven't played with this for years, so I have no idea honestly.
Meanwhile I encountered problems with attached document, so i suspect the situation is changed from 2016, even in current Word support of these formatting in documents made with previous versions.

However, Range.[Shading.BackgroundPatternColor](https://learn.microsoft.com/en-us/office/vba/api/Word.Shading.BackgroundPatternColor) seems a good candidate to query for background.

I'll continue to investigate, these seem to be things quite important in school, for teachers and students.
Here is the document for testing, please download and try to unzip it.
[test.zip](https://github.com/nvaccess/nvda/files/6047289/test.zip)

@feerrenrut @leonardder Could you please test this issue?
Thanks!
cc @michaelDCurran 
Is anyone following this issue?

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
@@ -41,6 +41,7 @@
 from ..behaviors import EditableTextWithoutAutoSelectDetection
 from . import _msOfficeChart
 import locationHelper
+from enum import IntEnum
 
 #Word constants
 
@@ -219,6 +220,56 @@
 	wdThemeColorText2:msoThemeDark2,
 }
 
+
+# document useful values from:
+# https://learn.microsoft.com/en-us/office/vba/api/word.wdcolorindex
+class WinWordColorIndex(IntEnum):
+
+	wdBlack = 1
+	wdBlue = 2
+	wdBrightGreen = 4
+	wdDarkBlue = 9
+	wdDarkRed = 13
+	wdDarkYellow = 14
+	wdGray25 = 16
+	wdGray50 = 15
+	wdGreen = 11
+	wdPink = 5
+	wdRed = 6
+	wdTeal = 10
+	wdTurquoise = 3
+	wdViolet = 12
+	wdWhite = 8
+	wdYellow = 7
+
+
+# document useful values from:
+# https://learn.microsoft.com/en-us/office/vba/api/word.wdcolor
+class WinWordColor(IntEnum):
+
+	wdBlack = 0
+	wdBlue = 16711680
+	wdBrightGreen = 65280
+	wdDarkBlue = 8388608
+	wdDarkRed = 128
+	wdDarkYellow = 32896
+	wdGray25 = 12632256
+	wdGray50 = 8421504
+	wdGreen = 32768
+	wdPink = 16711935
+	wdRed = 255
+	wdTeal = 8421376
+	wdTurquoise = 16776960
+	wdViolet = 8388736
+	wdWhite = 16777215
+	wdYellow = 65535
+
+
+# map (highlighting) color index to color decimal value
+_colorIndexToColor: Dict[WinWordColorIndex, WinWordColor] = {
+	colorIndex.value: WinWordColor[colorIndex.name].value for colorIndex in WinWordColorIndex
+}
+
 wdRevisionTypeLabels={
 	# Translators: a Microsoft Word revision type (inserted content) 
 	wdRevisionInsert:_("insertion"),
@@ -327,6 +378,7 @@
 	"reportLineSpacing": 0x40000,
 	"reportSuperscriptsAndSubscripts": 0x80000,
 	"reportGraphics": 0x100000,
+	"reportHighlight": 0x200000,
 }
 formatConfigFlag_includeLayoutTables = 0x20000
 
@@ -876,6 +928,20 @@ def _normalizeFormatField(self,field,extraDetail=False):
 		color=field.pop('color',None)
 		if color is not None:
 			field['color']=self.obj.winwordColorToNVDAColor(int(color))
+		bgColor = field.pop('background-color', None)
+		if bgColor is not None:
+			field['background-color'] = self.obj.winwordColorToNVDAColor(int(bgColor))
+		hlColorIndex = field.pop('highlight-color-index', None)
+		if hlColorIndex is not None:
+			hlColor = None
+			try:
+				val = _colorIndexToColor[int(hlColorIndex)]
+				hlColor = self.obj.winwordColorToNVDAColor(val)
+			except (KeyError, ValueError):
+				log.debugWarning("highlight color error", exc_info=True)
+				pass
+			if hlColor is not None:
+				field['highlight-color'] = hlColor
 		try:
 			languageId = int(field.pop('wdLanguageId',0))
 			if languageId:
diff --git a/source/speech/speech.py b/source/speech/speech.py
--- a/source/speech/speech.py
+++ b/source/speech/speech.py
@@ -2442,6 +2442,17 @@ def getFormatFieldSpeech(  # noqa: C901
 				# Translators: Reported when text is no longer marked
 				else _("not marked"))
 			textList.append(text)
+		# color-highlighted text in Word
+		hlColor = attrs.get("highlight-color")
+		oldHlColor = attrsCache.get("highlight-color") if attrsCache is not None else None
+		if (hlColor or oldHlColor is not None) and hlColor != oldHlColor:
+			colorName = hlColor.name if isinstance(hlColor, colors.RGB) else hlColor
+			text = (
+				# Translators: Reported when text is color-highlighted
+				_("highlighted in {color}").format(color=colorName) if hlColor
+				# Translators: Reported when text is no longer marked
+				else _("not highlighted"))
+			textList.append(text)
 	if formatConfig["reportEmphasis"]:
 		# strong text
 		strong=attrs.get("strong")


Test patch: