infinite while loop for Find.Execute & Word.Range & Range.Hyperlink for VSTO C#

Hi All,

I'm stuck on a weird problem...for Word Add-In with VSTO for Visual Studio 2008 C#.

The code below is executed from the searchDoc method. What is currently happening, when the code is executed it finds the word 'test' and then bolds/italics/hyperlinks it..the second 'test' word is then found(e.g 'this is a test, test again')
and it is then again bold/italic/hyperlinked however, it keeps repeating this process for the last word that is found in the document.
The problem only occurs for this section of code :
    doc.Hyperlinks._Add(rngDoc, ref oAddress, ref missing);

If I comment that out, it works fine, its like the range is adusted from that hyperlink method. I can't figure out how to re-adjust the range or to terminate the loop?

Can anyone help?

See the code below:

 private void searchDoc()
        {
            bool wordFound = true;
            String url = "http://www.test.com";
            Object findWord = "test";
            Object forward = true;
            Object wrap = WdFindWrap.wdFindContinue;
            //Object wrap = Type.Missing;
            Object start = 0;
            int start1 = 0;

            Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            Object end = doc.Characters.Count;
            int end2 = doc.Characters.Count;
            doc.Paragraphs[1].Range.Find.ClearFormatting();

            Range rngDoc = doc.Range(ref start, ref end);
            Find fnd = rngDoc.Find;
            fnd.Forward = true;
            wordFound = executeFind(fnd, wrap, forward, findWord);
            while (fnd.Found)
            {

                MessageBox.Show("Text found.");
                rngDoc.Select();
                boldWord(doc, rngDoc, url, null);
                //start = rngDoc.End;
                //start1 = rngDoc.End+1;
                //end = doc.Characters.Count;
                //end2 = doc.Characters.Count;
                //rngDoc.Start = start1;
                //rngDoc.End = end2;
                //if (start1.Equals(end2))
                //{
                //    wordFound = false;
                //}
                wordFound = executeFind(fnd, wrap, forward, findWord);
            }
            }

  private void boldWord(Document doc, Range rngDoc, String url, String foundWord)
        {
            Object missing = System.Reflection.Missing.Value;
            Object oAddress = url;
            Object oRange = rngDoc;
            rngDoc.Bold = 1;
            rngDoc.Italic = 1;
            doc.Hyperlinks._Add(rngDoc, ref oAddress, ref missing);
        }
       private Boolean executeFind(Find find, Object wrapFind, Object forwardFind, Object findWord)
        {
            // Simple wrapper around Find.Execute:
            Object findText = findWord;
            Object matchCase = Type.Missing;
            Object matchWholeWord = Type.Missing;
            Object matchWildCards = Type.Missing;
            Object matchSoundsLike = Type.Missing;
            Object nmatchAllWordForms = Type.Missing;
            Object forward = forwardFind;
            Object wrap = Type.Missing;
            Object wrap = wrapFind;
            Object format = Type.Missing;
            Object replaceWithText = Type.Missing;
            Object replace = Type.Missing; //object replace = WdReplace.wdReplaceNone;
            Object matchKashida = Type.Missing;
            Object matchDiacritics = Type.Missing;
            Object matchAlefHamza = Type.Missing;
            Object matchControl = Type.Missing;

            return find.Execute(ref findText, 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);
        }


Thank you!!
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.

SriVaddadiCommented:
It is important to note that the Find criteria are cumulative, which means that criteria are added to previous search criteria. Clear formatting from previous searches by using the ClearFormatting method prior to the search.

Check this out

http://msdn.microsoft.com/en-us/library/f1f367bx(v=VS.80).aspx
0
lp84Author Commented:
I cleared the formatting but the same problem occurs?
0
SriVaddadiCommented:
Try changing fnd.Found to wordFound in your while condition
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

lp84Author Commented:
no still the same problem. I dont think its a problem with the loop. Like I said, when I comment out the
' doc.Hyperlinks._Add(rngDoc, ref oAddress, ref missing);' it works as desired. Its like what is being returned from this function is show how causing it to infinite loop?
0
Miguel OzSoftware EngineerCommented:
Change:
object wrap = WdFindWrap.wdFindContinue;
to
object wrap = WdFindWrap.wdFindStop;
0
lp84Author Commented:
Tried that already as well ... Tried it again anyway still the same problem :(
0
irudykCommented:
Maybe try something like the following:

 private void searchDoc()
        {
            bool wordFound = true;
            String url = "http://www.test.com";
            Object findWord = "test";
            Object forward = true;
            Object wrap = WdFindWrap.wdFindContinue;
            //Object wrap = Type.Missing;
            //Object start = 0;
            //int start1 = 0;

            Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            //Object end = doc.Characters.Count;
            //int end2 = doc.Characters.Count;
            //doc.Paragraphs[1].Range.Find.ClearFormatting();

            Selection rngDoc = doc.ActiveWindow.Selection;
            rngDoc.HomeKey(WdUnits.wdStory);
            //Range rngDoc = doc.Range(ref start, ref end);
            Find fnd = rngDoc.Find;
            fnd.Forward = true;
            wordFound = executeFind(fnd, wrap, forward, findWord);
            while (fnd.Found)
            {

                MessageBox.Show("Text found.");
                //rngDoc.Select();
                boldWord(doc, rngDoc, url, null);
                //start = rngDoc.End;
                //start1 = rngDoc.End+1;
                //end = doc.Characters.Count;
                //end2 = doc.Characters.Count;
                //rngDoc.Start = start1;
                //rngDoc.End = end2;
                //if (start1.Equals(end2))
                //{
                //    wordFound = false;
                //}
                wordFound = executeFind(fnd, wrap, forward, findWord);
            }
            }

  private void boldWord(Document doc, Selection rngDoc, String url, String foundWord)
        {
            Object missing = System.Reflection.Missing.Value;
            Object oAddress = url;
            Object oRange = rngDoc.Range();
            oRange.Bold = 1;
            oRange.Italic = 1;
            doc.Hyperlinks._Add(oRange, ref oAddress, ref missing);
        }
       private Boolean executeFind(Find find, Object wrapFind, Object forwardFind, Object findWord)
        {
            // Simple wrapper around Find.Execute:
            Object findText = findWord;
            Object matchCase = Type.Missing;
            Object matchWholeWord = Type.Missing;
            Object matchWildCards = Type.Missing;
            Object matchSoundsLike = Type.Missing;
            Object nmatchAllWordForms = Type.Missing;
            Object forward = forwardFind;
            Object wrap = Type.Missing;
            Object wrap = wrapFind;
            Object format = Type.Missing;
            Object replaceWithText = Type.Missing;
            Object replace = Type.Missing; //object replace = WdReplace.wdReplaceNone; 
            Object matchKashida = Type.Missing;
            Object matchDiacritics = Type.Missing;
            Object matchAlefHamza = Type.Missing;
            Object matchControl = Type.Missing;

            return find.Execute(ref findText, 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);
        }

Open in new window

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
lp84Author Commented:
Had to fix the code up a bit. BUT! That works! Don't really understand what that does though. Could you explain the difference between the code I made?
0
irudykCommented:
Sorry about the code (my C# skills are not the best). There were a couple of things that I modofied. The first was that I set the selection/focus within the document to the actual start of the document prior to performing the find. The other was to use a selection (rather than a range) object to base the find function on.
0
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
.NET Programming

From novice to tech pro — start learning today.