0

我想要做的是编写一个应用程序,它可以在 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 中尝试查找/替换一样。知道是否有另一种选择来解决这种特殊情况吗?

4

2 回答 2

0

尝试设置以下查找属性

myStoryRange.Find.MatchWildcards = false ;

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

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

于 2013-09-10T19:01:00.073 回答
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.
于 2021-02-05T17:48:16.723 回答