0

这是我第一次在这里提出问题,所以这里是:

我正在搜索具有“标题 1”或“标题 2”样式名称的 word 文档中的文本。找到后,我将一个新范围设置为该文本的开头和文档的结尾,并调用一个方法来查找该范围中的第一个表和数字列 1,如果第二列有文本。现在,当我的代码找到完全匹配的第一条记录时,我的问题就发生了。一切正常,但 Find.Execute 进入一个永远循环并一遍又一遍地寻找相同的文本。我希望它继续搜索直到文档结束。这是我的代码,提前致谢:

    public void TextFind(object findText, string reqCode)
    {
        Document doc = Application.ActiveDocument;
        Range docRange = doc.Sections[1].Range;
        var intCount = 0;
        docRange.SetRange(0, docRange.Characters.Count);
        if (findText == null) throw new ArgumentNullException("findText");
        docRange.Find.Execute(findText, Forward: true, Wrap: WdFindWrap.wdFindStop);
        while (docRange.Find.Found)
        {
            intCount++;
            Style style = docRange.get_Style();
            var styleName = style.NameLocal;
            if (styleName == "Heading 1" || styleName == "Heading 2")
            {
                var endRange = doc.Sections[1].Range;
                var docRange2 = docRange;
                docRange2.SetRange(docRange.Start, endRange.End);
                ApplyNumberingToTable(docRange2, reqCode);
            }
            docRange.Find.Execute(findText, Forward: true, Wrap: WdFindWrap.wdFindStop);
        }

        MessageBox.Show(intCount.ToString());
    }

并且是我试图诊断问题的尝试intCountMessageBox

尝试第二个包括:

所以我仍然卡住了,我试图将搜索到的范围添加到列表中,并在列表上运行 foreach,但每次范围更改时,列表中对该范围的引用也会更改。我一生都无法弄清楚 t5o 如何在我的 while 循环中实例化 Range。这是我的代码:

public void TextFind(object findText, string reqCode)
    {
        Document doc = Application.ActiveDocument;
        Range docRange = doc.Sections[1].Range;
        var intCount = 0;
        if (findText == null) throw new ArgumentNullException("findText");
        var rangelist = new List<Range>();
        var i = 0;
        while (docRange.Find.Execute(findText, Forward: true, Wrap: WdFindWrap.wdFindStop))
        {
            rangelist = new List<Range> {docRange};
            intCount++;
        }
        foreach ( var range in rangelist)
        {
            Style style = range.get_Style();
            var styleName = style.NameLocal;
            if (styleName != "Heading 1" && styleName != "Heading 2") continue;
            var endRange = doc.Sections[1].Range;
            range.SetRange(range.Start, endRange.End);
            ApplyNumberingToTable(range, reqCode);
        }
        if (intCount == 0) return;
        MessageBox.Show(rangelist.ToString());
    }

仍在寻找解决此问题的方法。谢谢,

4

1 回答 1

0

根据此处的文档,该方法本身返回一个布尔值,说明它是否已成功找到搜索。也许您应该捕获此变量并将其用于您的 while 语句,而不是 Found 属性。我目前无法对此进行测试;今天晚些时候将返回结果。

也许尝试:

        while (docRange.Find.Execute(findText, Forward: true, Wrap: WdFindWrap.wdFindStop))
        {
            intCount++;
            Style style = docRange.get_Style();
            var styleName = style.NameLocal;
            if (styleName == "Heading 1" || styleName == "Heading 2")
            {
                var endRange = doc.Sections[1].Range;
                var docRange2 = docRange;
                docRange2.SetRange(docRange.Start, endRange.End);
                ApplyNumberingToTable(docRange2, reqCode);
            }
            docRange.Find.Execute(findText, Forward: true, Wrap: WdFindWrap.wdFindStop);
        }
于 2012-09-18T15:51:04.893 回答