查找和替换Word互操作全字匹配

时间:2013-09-10 18:31:05

标签: c# replace ms-word find interop

我要做的是编写一个可以找到&替换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

显然匹配“整个单词”不包括_'s。它与您在Microsoft Word中尝试查找/替换相同。是否有其他选择来解决这一特定情况?

2 个答案:

答案 0 :(得分:0)

尝试设置以下查找属性

myStoryRange.Find.MatchWildcards = false ;

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

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

答案 1 :(得分:0)

与其说是一个答案,不如说是一种帮助其他像我一样正在寻找 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.
相关问题