【问题标题】:C# Word vsto Add in Get Text of visible screen onlyC# Word vsto Add in 仅获取可见屏幕的文本
【发布时间】:2019-11-19 09:02:10
【问题描述】:

我正在尝试从活动文档 MS word 的当前可见屏幕(页面)中获取文本和范围。

我尝试了下面的代码,它在新文档中运行良好。

    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

这是我使用的原生 mwthod

[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);

当我打开保存的文档并尝试获取可见区域文本时出现问题。那时ActiveWindow.RangeFromPoint(x,y) 每次都会返回r1range 0,0)r2range 1,1)(如上所述)。

谁能帮我解决这个问题?

【问题讨论】:

    标签: c# ms-word word-addins word-interop


    【解决方案1】:

    我以前也遇到过同样的问题。我建议您尝试以下解决方案。我以为您因为只有单页文档而面临问题,可能这就是“r2.Start”将返回 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;
    

    希望对您有所帮助!

    【讨论】:

    • 是的,这可以解决当前的问题,但它是正确的解决方案吗?因为当文档为空白并且用户尝试键入某些内容时,为 r2.start 范围设置硬代码条件可能是错误的。
    猜你喜欢
    • 1970-01-01
    • 2018-08-22
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多