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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.