window.getRangeFromPoints() doesn't return correct range in some cases.
We are trying to fetch content from visible area, but this doesn't return correct range in some cases. What we have observed is that this might be due endnote/footnote reference in b/w but it works fine with endnote/footnote in some cases.
How do we fetch text in visible area?
First we fetch window rectangle using following code:
public static RECT GetWordDocumentRect(Window activeWindow, bool isCleintRect = false)
{
int hwnd = activeWindow.Hwnd;
RECT result = new RECT();
if (hwnd != 0)
{
var h = FindWindowExW((IntPtr)hwnd, new IntPtr(0), "_WwF", "");
h = FindWindowExW(h, new IntPtr(0), "_WwB", activeWindow.Caption);
h = FindWindowExW(h, new IntPtr(0), "_WwG", "Microsoft Word Document");
if (isCleintRect)
{
GetClientRect(h, out result);
}
else
{
GetWindowRect(h, out result);
}
return result;
}
return result;
}
Then we fetch range from window using window rect:
public static Range GetDocumetRange(RECT windowRect, Microsoft.Office.Interop.Word.Window activeWindow)
{
Range r1 = activeWindow.RangeFromPoint((int)windowRect.Left, (int)windowRect.Top);
Range r2 = activeWindow.RangeFromPoint((int)windowRect.Right, (int)windowRect.Bottom);
return activeWindow.Document.Range(r1.Start, r2.End);
}
So if there are 70 characters in visible and and if I have endnote reference in b/w then r2.End gives 12(truncates the range) but if I remove endnote reference then it gives r2.End 70.
Doc:
What we are doing wrong? OR missing something? OR Any documentation for calculating range correctly?