window.getRangeFromPoints() doesn't return correct range in some cases.

Priyank Kotiyal 6 Reputation points
2022-02-23T14:56:50.717+00:00

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:
177195-image.png

What we are doing wrong? OR missing something? OR Any documentation for calculating range correctly?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,781 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
812 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.