How to use find.Execute for VSTO in C#

Hi All,

Have a question on how to implement the find.execute method in VSTO for C# .NET 3.5 in VS 2008 for the "Add-in" feature.

I would like to find a certain text in the current document open, and once the text is found, I would like to insert a hyperlink on that text. I've created the add-in and the tool bar to initiate it. I just need help on how to actually use the method, and how to put a hyperlink on the word found.

I'm trying to use this way...but i'm not sure what to put in the parameters and such:

            Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1].Range.Find.Execute(ref findText, ref missing, ref matchAllWord,ref missing, ref missing, ref missing, ref forward,ref missing, ref missing, ref replaceText, ref replaceAll,ref missing, ref missing, ref missing, ref missing);

Thanks all
lp84Asked:
Who is Participating?
 
Miguel OzSoftware EngineerCommented:
Complementing my previous comment, please change code as follow:
object wrap = WdFindWrap.wdFindStop;
and replace the if with a while loop (no need for else)
while (rng.Find.Execute(ref findWord, ref matchCase,
                    ref matchWholeWord, ref matchWildCards,
                    ref matchSoundsLike, ref nmatchAllWordForms,
                    ref forward, ref wrap, ref format,
                    ref replaceWithText, ref replace,
                    ref matchKashida, ref matchDiacritics,
                   ref matchAlefHamza, ref matchControl))
                {
                    MessageBox.Show("Text found.");

                    rng.Bold = 1;
                    rng.Italic = 1;
                    object oRange = rng;
                    Globals.ThisAddIn.Application.ActiveDocument.Hyperlinks._Add(rng, ref oAddress, ref missing);
                }

Notice that:
1) I delete                     rng.Select();
the reason is that you do not need this line and it may create an infinite loop.
2) add the hyperlinks to the document itself

Note: Delete this duplicate line as well:
Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1].Range.Find.ClearFormatting();
0
 
Miguel OzSoftware EngineerCommented:
It seems Ok but I would use it as:
Word.Find searchRange = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1].Range.Find;
if (searchRange.Execute(ref findText, ref missing, ref matchAllWord,ref missing, ref missing, ref missing, ref forward,ref missing, ref missing, ref replaceText, ref replaceAll,ref missing, ref missing, ref missing, ref missing))
{
    //Do something as text has been found/replaced
}
else
{
    //Do something as text has not been found/replaced
}
0
 
lp84Author Commented:
does anyone have any examples?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
lp84Author Commented:
Was able to get it working like this:

    private void ReplaceRange()
        {
         
            object findWord = "test";
            object missing = System.Reflection.Missing.Value;
            Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1].Range.Find.ClearFormatting();
           
            if (Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1].Range.Find.Execute(ref findWord,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing))
            {
                MessageBox.Show("Text found.");
            }
            else
            {
                MessageBox.Show("The text could not be located.");
            }

    Just need some help creating a hyperlink on that word found now.. and also how can I declare the object 'Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1]' and use it throughout the code? Instead of calling it each time?

Thanks..
0
 
Miguel OzSoftware EngineerCommented:
using Word = Microsoft.Office.Interop.Word;
Word.Paragraph myParagraph = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1];

Regarding hyperlink, what do you mean? the word will be converted to hyperlink, if so what is the address the hyperlink will point to? (Describe what to link)
0
 
lp84Author Commented:
 private void ReplaceRange( object findText)
        {
         
            object findWord = "testword";
            object replaceWithText = false;
            object missing = System.Reflection.Missing.Value;
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object nmatchAllWordForms = 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;
            int intFound = 0;
            object wrap = WdFindWrap.wdFindContinue;
            Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1].Range.Find.ClearFormatting();
            Object oAddress = "http://www.test.com";
            Range rng = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1].Range;
            rng.Find.ClearFormatting();
            rng.Find.Forward = true;
            rng.Find.Text = "identification";
                if (rng.Find.Execute(ref findWord, ref matchCase,
                    ref matchWholeWord, ref matchWildCards,
                    ref matchSoundsLike, ref nmatchAllWordForms,
                    ref forward, ref wrap, ref format,
                    ref replaceWithText, ref replace,
                    ref matchKashida, ref matchDiacritics,
                   ref matchAlefHamza, ref matchControl))
                {
                    MessageBox.Show("Text found.");

                    rng.Select();
                    rng.Bold = 1;
                    rng.Italic = 1;
                    object oRange = rng;
                    rng.Hyperlinks.Add(rng, ref oAddress, ref missing, ref missing, ref missing, ref missing);
                }
                else
                {
                    MessageBox.Show("The text could not be located.");
                }
}

I got it all working with this - but I have a problem. Whenever I search for text i.e

"this is a testword, try test testword again"

it will only hyperlink the first 'testword' and not the second. How can I parse through the entire document?
0
 
lp84Author Commented:
one more thing to add...
if I change the above code to the following:

replaceWithText = testword;
object replace = WdReplace.wdReplaceAll;


the whole sentence is hyper linked instead of each 'testword'.
0
 
Miguel OzSoftware EngineerCommented:
Change:
object wrap = WdFindWrap.wdFindContinue;
to
object wrap = WdFindWrap.wdFindStop;
and replace the if with a while loop (no need for else)
while (rng.Find.Execute(ref findWord, ref matchCase,
                    ref matchWholeWord, ref matchWildCards,
                    ref matchSoundsLike, ref nmatchAllWordForms,
                    ref forward, ref wrap, ref format,
                    ref replaceWithText, ref replace,
                    ref matchKashida, ref matchDiacritics,
                   ref matchAlefHamza, ref matchControl))
                {
                    MessageBox.Show("Text found.");

                    rng.Select();
                    rng.Bold = 1;
                    rng.Italic = 1;
                    object oRange = rng;
                    rng.Hyperlinks.Add(rng, ref oAddress, ref missing, ref missing, ref missing, ref missing);
                }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.