• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2653
  • Last Modified:

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!!
0
lp84
Asked:
lp84
  • 4
  • 2
  • 2
  • +1
1 Solution
 
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now