Search code examples
c#ms-wordoffice-interop

Linking a string in Word VSTO addin Word.interop 2


In the code I am searching string and putting link beneath that string. But the problem is, that it is working only for the first word in the paragraph. How can I do foreach statement rightly?

using Word = Microsoft.Office.Interop.Word;

private Word.Range FindAndReplace(Word.Range rngToSearch, object findText, object replaceWithText)
        {
            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 = 1;

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

        private void Button3_Click(object sender, RibbonControlEventArgs e)
        {
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            Word.Range rng = doc.Content;
            string searchTerm = @"<[0-9]-[0-9]{1;}-[0-9]{1;}/[0-9]{1;}>";
            string hyperlink = "";  //put your hyperlink stuff here

            foreach (Word.Paragraph paragraph in doc.Paragraphs)
            {
                Word.Range rngFound = FindAndReplace(rng, searchTerm, ""); //searching and wrapping.

                if (rngFound != null)
                {
                Word.Hyperlink hp = (Word.Hyperlink)
                    rngFound.Hyperlinks.Add(rngFound, hyperlink + rngFound.Text);
                }
            }
 }

1 )I have tired: foreach (Word.Range docRange in doc.Words) {Word.Range rngFound = FindAndReplace(docRange, searchTerm, "") It takes words one by one. And it is wery slow. 2) Also tried to use the Selection:

private Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText)
        {
-----------------------------------------------------------------------------
foreach (Word.Paragraph paragraph in doc.Paragraphs)
            {
                Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping. 

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

But now it does one word with one click. And in the end I solved it with that method. Look at my last comment.

3) I also tried:

foreach (Word.Paragraph paragraph in doc.Paragraphs)
                {
                    Word.Range rngFound = FindAndReplace(paragraph.Range, searchTerm, ""); //searching and wrapping.

                    if (rngFound != null)
                    {
                    Word.Hyperlink hp = (Word.Hyperlink)
                        rngFound.Hyperlinks.Add(rngFound, hyperlink + rngFound.Text);
                    }

And it does the work with the first words in paragraphs.

What I need to do to run it in all of my document Range.


Solution

  • Thanks. At the end I have>

    Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText) //funcija poiska 4erez range
            {
                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;
            }
    
            private void Button3_Click(object sender, RibbonControlEventArgs e)
            {
                int WordsCount = 0;
    
                int counter = 0;
                Word.Application app = Globals.ThisAddIn.Application;
                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                Word.Range rng = doc.Content;
                string searchTerm = @"<[0-9]-[0-9]{1;}-[0-9]{1;}/[0-9]{1;}>";
                string hyperlink = "google.com";  //
                string s = rng.Text; // Pushing doc text to the string
                Regex regex = new Regex(@"\d*-\d*-\d*/\d*"); 
                WordsCount = regex.Matches(s).Count; // Using regex getting count of searchable words 
    
    
    
    
    
                while (WordsCount >= counter ) // knowing count of words we know how much iterations we need to do.  
                    foreach (Word.Section paragraph in doc.Sections)
                {
                        Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping.
    
                        if (rngFound != null)
                    {
    
                            rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + rngFound.Text);
    
                    }counter++; // counting iterations
    
    
                    }
            }
    

    And it is working. Thankyou Cindy for your good advice.