Abhay Kalariya
Abhay Kalariya

Reputation: 61

C# Word vsto Add in Get Text of visible screen only

I'm trying to obtain text and range from currently visible screen(Page) of active document MS word.

I have try bellow code which is working fine with new document.

    IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
    h = NativeMethodsActiveScreen.FindWindowExW(h, new IntPtr(0), "_WwF", "");
    h = NativeMethodsActiveScreen.FindWindowExW(h, new IntPtr(0), "_WwB", null);
    h = NativeMethodsActiveScreen.FindWindowExW(h, new IntPtr(0), "_WwG", null);
    NativeMethodsActiveScreen.tagRECT t = new NativeMethodsActiveScreen.tagRECT();
    NativeMethodsActiveScreen.GetWindowRect(h, out t);
    Range r1 = Globals.ThisAddIn.Application.ActiveWindow.RangeFromPoint(t.left, t.top);
    Range r2 = Globals.ThisAddIn.Application.ActiveWindow.RangeFromPoint(t.right, t.bottom);
    Range r = Globals.ThisAddIn.Application.ActiveDocument.Range(r1.Start, r2.Start);
   //here r1 and r2 return wrong value in open document case as describe bellow

Here is the native mwthod which i have used

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
     public struct tagRECT
     {
          public int left;
          public int top;
          public int right;
          public int bottom;
     }
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "FindWindowExW")]
public static extern System.IntPtr
                        FindWindowExW([System.Runtime.InteropServices.InAttribute()]
                            System.IntPtr hWndParent, [System.Runtime.InteropServices.InAttribute()]
                            System.IntPtr hWndChildAfter, [System.Runtime.InteropServices.InAttribute()]
                            [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
                            string lpszClass, [System.Runtime.InteropServices.InAttribute()]
                            [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
                            string lpszWindow);

[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetWindowRect")]
public static extern System.IntPtr GetWindowRect([System.Runtime.InteropServices.InAttribute()]
                            System.IntPtr hWndParent, out tagRECT lpRect);

A problem occurs when I open saved document and try to get visible area text. At that time ActiveWindow.RangeFromPoint(x,y) returns for r1: range 0,0) and for r2: range 1,1) (as listed above) every time.

can any one help me to resolve this problem?

Upvotes: 1

Views: 383

Answers (1)

jignesh sinojiya
jignesh sinojiya

Reputation: 31

I have faced same issue previously. I suggest you to try below solution. I thought you were facing issue due to only single page document, might be that's why "r2.Start" will return 1.

Range r1 = (Range)RibbonHelper.SharedApplicationInstance.Application.ActiveWindow.RangeFromPoint((int)t.left, (int)t.top);
                Range r2 = (Range)RibbonHelper.SharedApplicationInstance.Application.ActiveWindow.RangeFromPoint((int)t.right, (int)t.bottom);
                Range r = RibbonHelper.SharedApplicationInstance.ActiveDocument.Range(r1.Start, r2.Start);

                Range FullDocRange = RibbonHelper.SharedApplicationInstance.ActiveDocument.Range();
                if (r2.Start <= 1)
                {
                    r = FullDocRange;
                }
                RibbonHelper.VisibleAreaRange = r;

Hope it will help!

Upvotes: 2

Related Questions