我的MS Word文档看起来像这样
现在我想找到哪个表包含短语“/ CCID_ntRow /”。从文档结构中可以看出,它是第二个表。但是我遍历的每个表,Find.Execute的返回都返回true。 这是我的代码:
int whichTable = 1; //starting index is 1, not 0
do {
doc.Tables[whichTable].Range.Find.ClearFormatting();
doc.Tables[whichTable].Range.Find.Wrap = MSWord.WdFindWrap.wdFindContinue;
doc.Tables[whichTable].Range.Find.Text = "/CCID_ntRow/";
doc.Tables[whichTable].Range.Select();
isFound = doc.Tables[whichTable].Range.Find.Execute(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, ref missing);
if (isFound == true) {
//everytime, isFound returns true. The right must be 2
break;
}
whichTable++;
} while (true);
请帮我解决这个问题。提前谢谢
答案 0 :(得分:4)
我们可以将所有元素表转换为类型为Table的IEnumerable,然后我们可以继续使用LINQ查询来获取所需的表。
var requiredTable = Tables.Cast<Table>().FirstOrDefault(t => t.Range.Text.Contains("SOME TEXT"));
答案 1 :(得分:1)
您的代码中的Range.Find.Execute错误。尝试下面的工作
isFound = doc.Tables[whichTable].Range.Find.Execute("/CCID_ntRow/", 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);