Link to home
Start Free TrialLog in
Avatar of lp84
lp84

asked on

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
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

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
}
Avatar of lp84
lp84

ASKER

does anyone have any examples?
Avatar of lp84

ASKER

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..
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)
Avatar of lp84

ASKER

 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?
Avatar of lp84

ASKER

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'.
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);
                }
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial