我正在尝试编写一个应用程序来搜索单词文档以查找所有出现的,其中some_text是<和>之间的任何字符串。当我找到每场比赛时,我想与每场比赛一起存储/显示/做某事。
这是我到目前为止的内容:
Word._Application word = new Word.Application();
Word.Documents d = word.Documents;
Word._Document doc;
doc = d.Open(strFileName);
doc.Activate();
foreach (Word.Range myStoryRange in doc.StoryRanges)
{
myStoryRange.Find.MatchWildcards = true;
myStoryRange.Find.Text = "[<]*[>]";
myStoryRange.Find.Execute();
// Somehow get the result string that matched the wildcard
}
事实证明,已为每个找到的字符串重新定义了范围。您可以按以下方式访问找到的每个文本:
rng.Text
您可以在更大的范围内找到找到的文本字符位置:
rng.Start
rng.End
因此,我可以通过在Find循环中声明仅包含找到的字符串的本地Range来做到这一点。我用DocProperty替换了每个文本,但是您可以用它做任何您想做的事情:
Word.Range rng = this.Content;
rng.Find.MatchWildcards = true;
rng.Find.Text = "[<]*[>]";
while (rng.Find.Execute())
{
// create a local Range containing only a single found string
object cstart = rng.Start;
object cend = rng.End;
Word.Range localrng = this.Range(ref cstart, ref cend);
// replace the text with a custom DocProperty
Word.Field newfld = localrng.Fields.Add(localrng, Word.WdFieldType.wdFieldDocProperty, "MyDocProp", false);
localrng.Fields.Update();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句