【问题标题】:How can I search only in users selected Selection如何仅在用户选择的选择中搜索
【发布时间】:2019-11-20 16:30:42
【问题描述】:

我知道如何搜索整个文档。我正在使用 foreach(doc.Sections 中的 Word.Section 段落)。但是如果我只想在选择中搜索呢?假设用户将选择文本然后进行搜索。然后我们只想在选择中搜索。我怎么能用 Foreach sicle 做到这一点? 现在我有这个代码:

  app.Selection.HomeKey(Unit: WdUnits.wdStory);
        while (WordsCount >= 1)
           foreach (Word.Section paragraph in doc.Sections)
            {

                Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping.


                if (rngFound != null)
                {
                    string foundNr = rngFound.Text;
                    string hyperlinkNr = foundNr.Replace((char)30, (char)45);

                    rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + hyperlinkNr);

                }
                WordsCount--;

我知道有多少字(我在找什么)。但是我怎么能说搜索功能我们只需要在选择中搜索。或该选择的范围。

这是我的搜索功能:

Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText) //Find function
        {
            bool found = false;
            //options
            object matchCase = false;
            object matchWholeWord = true;
            object matchWildCards = true;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = false;
            object wrap = Word.WdFindWrap.wdFindStop; ;

            //execute find and replace
            found = rngToSearch.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
                ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
            if (!found)
            {
                rngToSearch = null;
            }

            return rngToSearch;
        }

谢谢辛迪。我正在使用选择,因为如果我将使用范围,那么它将找到第一个搜索词并在其上循环。我有对象 forward = true;在我的搜索功能中。 (所以这种行为对我来说很奇怪)

private void button4_Click(object sender, RibbonControlEventArgs e)
        {
            string hyperlink = "https://Test";
            string searchTerm =
                @"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})/([0-9]{1;4})>"; // testing with  2-19-1023/47 2-19-1023/49


            Word.Application app = Globals.ThisAddIn.Application;
            Word.Range rngSel = app.Selection.Range;
            Word.Range rngSearch = rngSel.Duplicate;
            Word.Range rngFound = FindAndReplace(rngSearch, searchTerm, "");
            while (rngFound != null)
            {
                string foundNr = rngFound.Text;
                string hyperlinkNr = foundNr.Replace((char)30, (char)45);
                rngFound.Hyperlinks.Add(rngFound, hyperlink + hyperlinkNr);
                rngSearch.Start = rngFound.End;
                rngSearch.End = rngSel.End;
                rngFound = FindAndReplace(rngSearch, searchTerm, "");
                File.AppendAllText(@"C:\install\CSharp\tulemus.txt", $"File name is: {rngFound.Text}" + Environment.NewLine);
            }

我在我的测试文本文件中获得了无穷无尽的第一个可搜索字符串。你知道我该如何克服吗?

【问题讨论】:

    标签: c# search ms-word selection office-interop


    【解决方案1】:

    只要每个搜索周期都限制在特定范围内,就应该只在该范围内执行搜索。这是通过将选择分配给Range 对象并使用其他Range 对象来完成的

    在下面的示例中,根据问题中的代码,使用当前选择声明并实例化了一个 Range 对象。然后将第二个Range 设置为第一个范围的Duplicate 属性(这会创建一个“浅拷贝”,没有指向原始范围的链接)。

    然后将第二个范围传递给函数FindAndReplace,该函数返回一个第三个​​ Range,其中包含找到的内容。然后对其进行处理。

    最后,将搜索范围的起点设置为找到范围的终点(这样搜索从最后一个“命中”的末尾开始),并设置了搜索的终点Range到原始Range 的终点并重复搜索。

    注意整个变化,调整为使用 Range 而不是 Selection 对象。

    Word.Range rngSel = app.Selection;
    Word.Range rngSearch = rngSel.Duplicate;
    Word.Range rngFound = FindAndReplace(rngSearch, searchTerm, ""); 
    while (rngFound != null)   
    {
        string foundNr = rngFound.Text;
        string hyperlinkNr = foundNr.Replace((char)30, (char)45);
        rngFound.Hyperlinks.Add(rngFound, hyperlink + hyperlinkNr);
        rngSearch.Start = rngFound.End
        rngSearch.End = rngSel.End;
        rngFound = FindAndReplace(rngSearch, searchTerm, "");
    }
    
    Word.Range FindAndReplace(Word.Range rngToSearch, object findText, object replaceWithText) //Find function
    {
    

    【讨论】:

    • 谢谢辛迪!请查看我编辑的问题。该代码不适用于 Range 它卡在第一个可搜索的单词中并循环。我还有一些其他项目也无法使用 Range,因为它不会更进一步。我不知道为什么会这样。
    【解决方案2】:

    我解决了。 1.第一件事,我不知道的是,如果我们正在替换,搜索和替换功能会向前推进。但是,如果我们不替换,而只是搜索,那么使用范围属性将无法正常工作。 2. 我设法使用 Rgegex 制作了自己的方法。我没有设置我的选择的起点和终点,而只是计算可搜索的单词。也许这不是最好的解决方案,但它对我有用。 这是代码:

    private void button5_Click(object sender, RibbonControlEventArgs e)
            {
                int WordsCount2 = 0;
    
    
                string hyperlink = "https://Test";
                string searchTerm =
                    @"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})/([0-9]{1;4})>"; // testing with  2-19-1023/47 2-19-1023/49
    
    
                Word.Application app = Globals.ThisAddIn.Application;
                Word.Range rngSel = app.Selection.Range;
                string s = rngSel.Text;
                Word.Range rngSearch = rngSel.Duplicate;
                Regex regex2 = new Regex(@"\d*(-|\036)\d*(-|\036)\d*/\d*");
                if (regex2.Matches(s).Count != 0)
                {
                    WordsCount2 = regex2.Matches(s).Count;
    
    
    
                    while (WordsCount2 >= 1)
                    {
                        Word.Range rngFound = FindAndReplace(rngSel, searchTerm, "");
    
                        if (rngFound != null) { 
                            string foundNr = rngFound.Text;
                        string hyperlinkNr = foundNr.Replace((char)30, (char)45);
                        rngFound.Hyperlinks.Add(rngFound, hyperlink + hyperlinkNr);
    
    
                        if (rngFound != null)
                        {
                            FindAndReplace2(rngSel, searchTerm, rngFound.Text.Replace((char)45, (char)30).Replace((char)32, (char)160));
                            //   File.AppendAllText(@"C:\install\CSharp\tulemus.txt", $"File name is: {rngFound.Text}" + Environment.NewLine)
    
                        }
                        WordsCount2--;
                    }}
    
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多