简体   繁体   English

如何仅在用户选择的选择中搜索

[英]How can I search only in users selected Selection

I know how to search through entire document.我知道如何搜索整个文档。 I am using foreach (Word.Section paragraph in doc.Sections).我正在使用 foreach(doc.Sections 中的 Word.Section 段落)。 But what if I want to search only in Selection.但是如果我只想在选择中搜索呢? Lets assume that user will select the text and then make a search.假设用户将 select 文本然后进行搜索。 Then we want to search only in the selection.然后我们只想在选择中搜索。 How can I do that with Foreach sicle?我怎么能用 Foreach sicle 做到这一点? Rigt now I have this code: Rigt 现在我有这个代码:

  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--;

I know how many words are there(what I am looking for).我知道有多少字(我在找什么)。 But how can i say to search function that we need to search only in selection.但是我怎么能说搜索 function 我们只需要在选择中搜索。 Or a range from that selection.或该选择的范围。

And this is my search function:这是我的搜索 function:

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;
        }

Thanks Cindy.谢谢辛迪。 I am using the selection because if I will use the Range, then it will find the first search word and loop on it.我正在使用选择,因为如果我将使用范围,那么它将找到第一个搜索词并在其上循环。 And I have object forward = true;我有 object forward = true; in my search function.在我的搜索 function 中。 (so that behavior is strange for me) (所以这种行为对我来说很奇怪)

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);
            }

And I am getting endless first searchable string in my test text file.而且我在测试文本文件中得到了无穷无尽的第一个可搜索字符串。 Do u know how can I overcome that?你知道我该如何克服吗?

As long as each search cycle is limited to the specific range, the search should be performed in that range, only.只要每个搜索周期都限制在特定范围内,就应该只在该范围内执行搜索。 This is done by assigning the selection to a Range object and working with additional Range objects这是通过将选择分配给Range object 并使用其他Range对象来完成的

In the following example, based on the code in the question, a Range object is declared and instantiated with the current selection.在以下示例中,根据问题中的代码,使用当前选择声明并实例化Range object。 A second Range is then set to the Duplicate property of the first range (this creates a "shallow copy" with no link to the original).然后将第二个Range设置为第一个 Range 的Duplicate属性(这将创建一个“浅副本”,没有指向原始范围的链接)。

The second range is then passed to the function FindAndReplace , which returns a third Range containing what was found.然后将第二个范围传递给 function FindAndReplace ,它返回包含找到的内容的第三个Range This is then processed.然后对其进行处理。

Finally, the Start point of the search range is set to the end point of the found range (so that the search starts from the end of the last "hit") and the End point of the search Range is set to the end-point of the original Range and the search repeated.最后,将搜索范围的起点设置为找到范围的终点(这样搜索从最后一个“命中”的末尾开始),并将搜索Range的终点设置为终点原始Range和重复搜索。

Note the changes throughout, adjusting to use Range rather than Selection objects.注意整个变化,调整为使用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
{

I solved it.我解决了。 1. The first thing, what I didn't know, that the search and replace function steps forward if we are replacing. 1.第一件事,我不知道的是,如果我们正在替换,搜索和替换 function 会向前推进。 But if we are not replacing, and just searching then it won't work using range property.但是,如果我们不替换,而只是搜索,那么使用范围属性将无法正常工作。 2. I amanaged to make my own method using Rgegex. 2. 我设法使用 Rgegex 制作了自己的方法。 I am not setting start and end points of my selection but simply counting searchable words.我没有设置我的选择的起点和终点,而只是计算可搜索的单词。 Maby this is not the best solution but it works for me.也许这不是最好的解决方案,但它对我有用。 And Here is the code:这是代码:

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--;
                }}

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在datagridview中的选定行上搜索字符串值? - How can I search for a string value on a selected row in the datagridview? 我该怎么办才能选择一个复选框? - How can I do so only one checkbox can be selected? 如何授权仅管理员用户可以访问的属性? - How can I authorize a property to be accessed by Admin users only? 如何限制用户仅使用Linq To SQL编辑数据? - How can I restrict users to only edit their data with Linq To SQL? 如何转换清单 <T> 仅具有选定参数的数据表 - How can I convert a List<T> to DataTable with only selected parameters 在WPF中,如何在DataGrid中仅允许选择单个单元格? - In WPF, how can I allow only a single cell to be selected in a DataGrid? 如何获得多重选择模式列表框的所选项目名称 - How can i get the selected items names of multi selection mode list box 如何获取Active Directory用户列表(仅Windows登录屏幕中显示的用户) - How can I get a list of Active Directory Users (Only the Users that appear in the windows Logon Screen) 如何获取列表本地Windows用户(仅Windows登录屏幕中显示的用户) - How can I get a list Local Windows Users (Only the Users that appear in the windows Logon Screen) 我如何仅过滤和搜索选定字段并排除 ASP.NET MVC 中的 null 文本框 - How do i filter and search selected fields only and exclude null textboxes in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM