c# - 查找包含特定文本的 MS Word 表

标签 c# interop ms-word vsto

我有一个如下所示的 MS Word 文档 enter image description here

现在我想找到哪个表包含短语“/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);

请帮我解决这个问题。预先感谢您

最佳答案

我们可以将所有元素Tables转换为Table类型的IEnumerable,然后我们可以继续使用LINQ查询来获取所需的表。

var requiredTable = Tables.Cast<Table>().FirstOrDefault(t => t.Range.Text.Contains("SOME TEXT"));

关于c# - 查找包含特定文本的 MS Word 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9842028/

相关文章:

c# - LoadLibrary 不会因缺少 DLL : why? 而失败

.net - 如何使用 Microsoft.Office.Interop.Word 以编程方式将文本框添加到另一个页面

excel - 使用VS2008和Office2007将Excel转换为PDF

javascript - 如何通过更改页面方向从 JavaScript 生成 Word 文档? (纵向到横向)

c# - 如何等待 PuppeteerSharp 中的异步函数评估

c# - haskell 处理速度比 C# (codeeval) 快得多

c# - 如何使用where条件

database - 动态Word文档

c# - 使用 C# 在 Word docx 中填充文档变量

c# - 调试 SQL 端散列失败