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.Applicat ion.Active Document.P aragraphs[ 1].Range.F ind.Execut e(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
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.Applicat
Thanks all
ASKER
does anyone have any examples?
ASKER
Was able to get it working like this:
private void ReplaceRange()
{
object findWord = "test";
object missing = System.Reflection.Missing. Value;
Globals.ThisAddIn.Applicat ion.Active Document.P aragraphs[ 1].Range.F ind.ClearF ormatting( );
if (Globals.ThisAddIn.Applica tion.Activ eDocument. Paragraphs [1].Range. Find.Execu te(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.Applica tion.Activ eDocument. Paragraphs [1]' and use it throughout the code? Instead of calling it each time?
Thanks..
private void ReplaceRange()
{
object findWord = "test";
object missing = System.Reflection.Missing.
Globals.ThisAddIn.Applicat
if (Globals.ThisAddIn.Applica
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.Applica
Thanks..
using Word = Microsoft.Office.Interop.W ord;
Word.Paragraph myParagraph = Globals.ThisAddIn.Applicat ion.Active Document.P aragraphs[ 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)
Word.Paragraph myParagraph = Globals.ThisAddIn.Applicat
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)
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.Applicat ion.Active Document.P aragraphs[ 1].Range.F ind.ClearF ormatting( );
Object oAddress = "http://www.test.com";
Range rng = Globals.ThisAddIn.Applicat ion.Active Document.P aragraphs[ 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?
{
object findWord = "testword";
object replaceWithText = false;
object missing = System.Reflection.Missing.
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.Applicat
Object oAddress = "http://www.test.com";
Range rng = Globals.ThisAddIn.Applicat
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?
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'.
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);
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Word.Find searchRange = Globals.ThisAddIn.Applicat
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
}