Link to home
Start Free TrialLog in
Avatar of lp84
lp84

asked on

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!!
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

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

ASKER

I cleared the formatting but the same problem occurs?
Try changing fnd.Found to wordFound in your while condition
Avatar of lp84

ASKER

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?
Change:
object wrap = WdFindWrap.wdFindContinue;
to
object wrap = WdFindWrap.wdFindStop;
Avatar of lp84

ASKER

Tried that already as well ... Tried it again anyway still the same problem :(
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada 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
Avatar of lp84

ASKER

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