【问题标题】:Select Text between two Strings C# Word Interop在两个字符串之间选择文本 C# Word 互操作
【发布时间】:2019-08-21 07:19:59
【问题描述】:

我想选择并复制两个给定字符串之间的范围。复制图片或表格也很重要。对我来说很难解释,我希望这个例子会有所帮助:

        Application word = new Application();
        word.Visible = true;
        object findtext = "Favour";
        object findtext2 = "valley";
        Document d2 = word.Documents.Open(@"Path");
        Range range = d2.Content;

        range.Find.Execute(ref findtext, ref missing, ref missing, ref 
        missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, ref missing, ref 
        missing);
        range.Select();
        .........

文字:
完全到达私人之间。虽然 elinor 直接喜欢这样,但还是挺喜欢的。合理的估计是我们自己合理地恳求我的改变。直接希望如此期待礼貌的山谷。谁的要求受得了它的感觉没有被破坏。谨慎你太他的行为感觉有限和。他失去了希望,所以面对现实。善良做了合适的学习投入。

我想要什么:
完全到达私人之间。 虽然 elinor 很直接,但还是喜欢这样。合理的估计是我们自己合理地恳求我的改变。直接希望如此期待礼貌谷。谁的要求受得了它的感觉没有被破坏。谨慎你太他的行为感觉有限和。他失去了希望,所以面对现实。善良做了合适的学习投入。

也许我很愚蠢,但我像一周一样工作。我所有的方法都不会复制所有的表格和图片等。

【问题讨论】:

  • 手动复制文字时,图片和表格是否包含在副本中?
  • 如果我选择一个范围内的所有内容,它将复制所有额外内容。当我将范围粘贴到另一个文档中时,一切都很好。

标签: c# ms-word word-interop


【解决方案1】:

这样的事情的关键是使用 两个 Range 对象:一个作为起点,另一个作为终点,这样可以扩展其中一个范围以包含另一个。我已经扩展了问题中的代码以进行说明。

请注意,通常不需要在 Word 文档之间(或之内)复制内容。 Range.FormattedText 属性可以快速传输格式化信息,而无需在剪贴板上放置任何内容。两种变体都包含在下面。

Application word = new Application();
word.Visible = true;
object findtext = "Favour";
object findtext2 = "valley";
Document d2 = word.Documents.Open(@"Path");
Range range = d2.Content;
Range rngEnd = null;

range.Find.Execute(ref findtext, ref missing, ref missing, ref missing, ref
  missing, ref missing, ref missing, ref missing, ref missing, 
  ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
rngEnd = range.Duplicate;

rngEnd.Find.Execute(ref findtext2, ref missing, ref missing, ref 
  missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
  ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
range.End = rngEnd.End;

targetDocument.Paragraphs.Last.Range.FormattedTExt = range.FormattedText;
//range.Copy();

【讨论】:

  • 谢谢这是个好主意,它激发了我的答案非常感谢
【解决方案2】:

我自己在互联网上找到了答案。我一直在寻找 C# 代码,但从未尝试过使用 VBA 代码。在可笑的 5 分钟后,我对一个我苦苦挣扎了两周的问题有了答案。

 Application word = new Application();
        word.Visible = true;

        Document doc= 
        word.Documents.Open(@"PATH");

        Range rng;
        Range rngText;
        object strStart = "STRING";
        object strEnd= "STRING";


        rng = doc.Content;
        rngText = doc.Range(0, 0);
        if(rng.Find.Execute(ref strStart))
        {
            rngText.SetRange(rng.Start,rng.End);
            rng.SetRange(rng.End, doc.Range().End   );
            if(rng.Find.Execute(ref strEnd))
            {
                rngText.SetRange(rngText.Start,rng.Start);

                rngText.Select();
                rngText.Font.Color = WdColor.wdColorAqua;
            }
        }

【讨论】:

    【解决方案3】:

    使用正则表达式完成任务

    String Prefix = "Favour";
    String Suffix = "valley";
    String RegexForm = String.Format(@"(?<={0}\s).*(?=\s{1})", Prefix, Suffix);
    
    Regex r = new Regex(RegexForm, RegexOptions.Compiled);
    
    String Data = "Arrived totally in as between private. Favour of so as on pretty though elinor direct. Reasonable estimating be alteration we themselves entreaties me of reasonably. Direct wished so be expect polite valley. Whose asked stand it sense no spoil to. Prudent you too his conduct feeling limited and. Side he lose paid as hope so face upon be. Goodness did suitable learning put.";
    
    Match m = r.Match(Data);
    
    Console.WriteLine("Match => " + Prefix + m.Value + Suffix);
    

    匹配包含原始字符串的起始索引和长度。

    如果您需要获得 2 个以上的结果,请使用 r.Matches 而不是 r.Match。

    希望这可行。 :)

    【讨论】:

    • 正则表达式如何帮助从 Word 中复制图像和表格?
    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 2013-12-31
    • 1970-01-01
    • 2016-07-25
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多