在word文档中查找范围内的匹配项
Find match in a range in word document
如果范围内有 2 个匹配项,是否有任何方法可以在指定范围内找到下一个匹配项。或者是否有任何方法可以获取所有匹配项的列表。
目前我正在使用下面的互操作方法来查找匹配项,但它只会突出显示第一个匹配项。
bool found = foundRange.Find.Execute(ref oFindText, 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);
谢谢。
查看内联评论:
//Get any range you want
var range = app.ActiveDocument.StoryRanges[WdStoryType.wdMainTextStory];
var document = range.Document;
//We want the variable range to continue refering to the same Range at all times
var foundRange = range.Duplicate;
if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)) {
Console.WriteLine("Found first");
//After using .Execute(), FoundRange has been set to the found text
Console.WriteLine(foundRange.Text);
}
else
{
Console.WriteLine("Didn't find first");
}
//Set foundRange to start at the character after the last find and to end where the original range ends
foundRange = document.Range(foundRange.Start + 1, range.End);
//Repeat. Obviously you could use some kind of loop
if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
{
Console.WriteLine("Found second");
Console.WriteLine(foundRange.Text);
}
else
{
Console.WriteLine("Didn't find second");
}
foundRange.Select(); //Just to verify. We don't need the selection object for anything
如果范围内有 2 个匹配项,是否有任何方法可以在指定范围内找到下一个匹配项。或者是否有任何方法可以获取所有匹配项的列表。 目前我正在使用下面的互操作方法来查找匹配项,但它只会突出显示第一个匹配项。
bool found = foundRange.Find.Execute(ref oFindText, 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);
谢谢。
查看内联评论:
//Get any range you want
var range = app.ActiveDocument.StoryRanges[WdStoryType.wdMainTextStory];
var document = range.Document;
//We want the variable range to continue refering to the same Range at all times
var foundRange = range.Duplicate;
if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)) {
Console.WriteLine("Found first");
//After using .Execute(), FoundRange has been set to the found text
Console.WriteLine(foundRange.Text);
}
else
{
Console.WriteLine("Didn't find first");
}
//Set foundRange to start at the character after the last find and to end where the original range ends
foundRange = document.Range(foundRange.Start + 1, range.End);
//Repeat. Obviously you could use some kind of loop
if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
{
Console.WriteLine("Found second");
Console.WriteLine(foundRange.Text);
}
else
{
Console.WriteLine("Didn't find second");
}
foundRange.Select(); //Just to verify. We don't need the selection object for anything