V. Z
V. Z

Reputation: 191

Linking a string in Word VSTO addin Word.interop

I have a word document with text and numbers. Numbers are written like that:

2-16-9035/88, 2-16-8344/41, 5-17-43/12, 2-15-5027/137

and so on. There might be a text between them. Like:

"There was a 2-16-9035/88 case in 2-16-8344/4".

I need to put a link beneath numbers. I have a code:

private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
        {
            //options
            object matchCase = false;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;                      //Function for searching and wrapping the text. It can also replace the wraped text.
            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
            doc.Selection.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);
        }

        private void Button3_Click(object sender, RibbonControlEventArgs e)
        {
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            Word.Application app = Globals.ThisAddIn.Application;
            foreach (Word.Paragraph paragraph in doc.Paragraphs)
            {
                FindAndReplace(app, "Google", ""); //searching and wrapping.
                Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;

                Microsoft.Office.Interop.Word.Hyperlink hp = (Microsoft.Office.Interop.Word.Hyperlink)
                    currentRange.Hyperlinks.Add(currentRange, "www.google.com");
            }
 }

Right now I can search a word "Google" wrap it and put a link on. Maby you will have an idea how can I search for thouse numbers. Like you see, they can be different lenght. What I need is to find a number, get a string(the number what is found) and add hyperlink: https://www.****asjaNr=$"{NumberWhatIsFound}"

Upvotes: 0

Views: 334

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

To find such pattern, it's necessary to use Word's wildcard search capabilities (sort of like RegEx). The following finds the samples posted in the question

<[0-9]-[0-9]{1;}-[0-9]{1;}/[0-9]{1;}>

beginning of a word | one instance of any character in the range 0 - 9 | a - character | any number of characters in the range 0 - 9 | a - character | any number of characters in the range 0 - 9 | a slash / | any number of characters in the range 0 - 9 | end of a word

You'll probably want to do further testing and, if you know any set of digits has a limited number of occurrences, set an upper limit (after the ; and before the closing brace }.

Also note that, depending on the regional settings in Windows you might need , in the place of ; in the braces.

I've adapted the code in the question to use wildcards. Since the calling procedure needs to work with the result range and text, I've changed the signature to return a Word.Range. It also accepts a Word.Range parameter and performs Range.Find instead of Selection.Find as it is more reliable and exacter to work with.

If Find is successful, the Range changes to the "found" term. Execute returns a boolean, indicating success or failure. This is checked and null is returned in case of failure.

I haven't tried to create the hyperlink you specify, since that wasn't the crux of the question (only one issue per question!), but I have indicated how to use the returned Range to generate it.

Note that I did test the wildcard search string, but adapting the code was done without testing since I don't have time to set up a project for it. So you may encounter a syntax error here or there...

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

Upvotes: 2

Related Questions