【问题标题】:Find and Replace Word Interop Whole Word Matching查找和替换单词互操作全字匹配
【发布时间】:2013-09-14 14:28:20
【问题描述】:

我想做的是编写一个应用程序,它可以在 Microsoft Word 文档中查找和替换所有出现的给定字符串。

到目前为止,我选择使用 Microsoft.Office.Interop.Word 程序集。它有效,但不完全是我想要的。问题是它正在匹配子字符串并替换它们。

到目前为止,这是我的代码:

foreach (DataRow drCrossWalkItem in dtCrossWalk.Rows)
{
    foreach (Word.Range myStoryRange in doc.StoryRanges)
    {
        myStoryRange.Find.MatchWholeWord = true;
        myStoryRange.Find.MatchPrefix = false;
        myStoryRange.Find.MatchSuffix = false;
        myStoryRange.Find.Text = drCrossWalkItem["strOldValue"].ToString();
        myStoryRange.Find.Replacement.Text = drCrossWalkItem["strNewValue"].ToString();
        myStoryRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
        myStoryRange.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
    }
}
doc.SaveAs2(strFinalPath);
doc.Close(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);

所以它在大多数情况下都能完美运行,但我遇到的问题是在以下示例中:

Document Text:
test_item_one
test_item_one_two_three

假设我想用“hello”替换“test_item_one”——在我当前的程序中,它会像这样替换两行:

Document Text:
hello
hello_two_three

显然匹配“整个单词”不包括_。这就像您在 Microsoft Word 中尝试查找/替换一样。知道是否有其他选项可以解决此特定情况吗?

【问题讨论】:

  • 我已经删除了我的答案,因为我认为你的态度非常不尊重和不值得帮助。不要指望我提供更多帮助。

标签: c# replace ms-word find interop


【解决方案1】:

尝试在查找属性之后设置

myStoryRange.Find.MatchWildcards = false ;

查看所有 Find 成员,看看是否有任何成员可以帮助您解决问题

http://msdn.microsoft.com/en-us/library/vstudio/microsoft.office.interop.word.find_members.aspx

【讨论】:

    【解决方案2】:

    与其说是一个答案,不如说是一种帮助其他像我一样正在寻找 Word 通配符匹配详细信息的人的方法。 它的文档很少,但我发现这个用于 javascript Office API 的链接也适用于 VSTO:

    https://docs.microsoft.com/en-us/office/dev/add-ins/word/search-option-guidance

    To find:                    Wildcard    Sample
    Any single character        ?   s?t finds sat and set.
    Any string of characters    *   s*d finds sad and started.
    The beginning of a word     <   <(inter) finds interesting and intercept, but not splintered.
    The end of a word           >   (in)> finds in and within, but not interesting.
    One of the specified characters     [ ]     w[io]n finds win and won.
    Any single character in this range  [-]     [r-t]ight finds right and sight. Ranges must be in ascending order.
    Any single character except the characters in the range inside the brackets     [!x-z]  t[!a-m]ck finds tock and tuck, but not tack or tick.
    Exactly n occurrences of the previous character or expression    {n}    fe{2}d finds feed but not fed.
    At least n occurrences of the previous character or expression   {n,}   fe{1,}d finds fed and feed.
    From n to m occurrences of the previous character or expression  {n,m}  10{1,3} finds 10, 100, and 1000.
    One or more occurrences of the previous character or expression  @      lo@t finds lot and loot.
    

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 1970-01-01
      • 2012-12-06
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多