小编Nac*_*cho的帖子

c#ms word获得可见文本

我正在尝试使用Microsoft.Office.Interop.Word获取C#中MS Word窗口中显示的文本.请注意,这不是整个文件甚至是页面; 只是用户看到的相同内容.

以下代码似乎适用于简单文档:

Application word = new Application();
word.Visible = true;
object fileName = @"example.docx";
word.Documents.Add(ref fileName, Type.Missing, Type.Missing, true);

Rect rect = AutomationElement.FocusedElement.Current.BoundingRectangle;

Range r1 = word.ActiveWindow.RangeFromPoint((int)rect.Left, (int)rect.Top);
Range r2 = word.ActiveWindow.RangeFromPoint((int)rect.Left + (int)rect.Width, (int)rect.Top + (int)rect.Height);
r1.End = r2.Start;

Console.WriteLine(r1.Text.Replace("\r", "\r\n"));
Run Code Online (Sandbox Code Playgroud)

但是,当文档包含其他结构(如标题)时,只返回部分文本.

那么,实现这一目标的正确方法是什么?

非常感谢!

更新的代码

Rect rect = AutomationElement.FocusedElement.Current.BoundingRectangle;

foreach (Range r in word.ActiveDocument.StoryRanges) {
    int left = 0, top = 0, width = 0, height = 0;
    try {
        try {
            word.ActiveWindow.GetPoint(out left, out top, out …
Run Code Online (Sandbox Code Playgroud)

c# office-interop

6
推荐指数
1
解决办法
1872
查看次数

如何获得TRichViewEdit单词坐标?

是否有可能在一个单词的屏幕中获得XY坐标TRichViewEdit?

如果是这样,怎么办呢?

delphi

1
推荐指数
1
解决办法
277
查看次数

标签 统计

c# ×1

delphi ×1

office-interop ×1