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

lp84
lp84 used Ask the Experts™
on
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!!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Author

Commented:
I cleared the formatting but the same problem occurs?
Try changing fnd.Found to wordFound in your while condition
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

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?
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Change:
object wrap = WdFindWrap.wdFindContinue;
to
object wrap = WdFindWrap.wdFindStop;

Author

Commented:
Tried that already as well ... Tried it again anyway still the same problem :(
Top Expert 2008
Commented:
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

Author

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?
Top Expert 2008

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial