none
searching until the end of the document RRS feed

  • Question

  • Hello,

    I'm using Find.Execute() to fine a string, and everytime it finds it has to do some modifications to the found item, and then continue to search.. I've written the code, but it modifyes only the first occurence and then stops..

    		string nextChar;
               
               object matchCase = true;
               object matchWholeWord = false;
               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 = 0;
               object wrap = 1;
    
               
               wordApp.Selection.Find.Execute(ref findLetter,
                   ref matchCase, ref matchWholeWord,
                   ref matchWildCards, ref matchSoundsLike,
                   ref nmatchAllWordForms, ref forward,
                   ref wrap, ref format, ref missing,
                   ref replace, ref matchKashida,
                   ref matchDiacritics, ref matchAlefHamza,
                   ref matchControl);
               if (wordApp.Selection.Find.Found)
               {
                   wordApp.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);
                   nextChar = wordApp.Selection.Characters[1].Text;
                   Range charF = wordApp.Selection.Characters[1].FormattedText;
                                 
                   switch (nextChar.ToLower()) {
                       case "a":
                       case "b":
                       case "c":
                       case "d":
                           
                               FindAndReplace();
                       
                 
    
                   }

    Tuesday, January 15, 2013 1:51 AM

Answers

  • Hi hurrem

    First, change the wrap argument to correspond to Word.WdFindWrap.wdFindStop. You have it set to wdFindContinue which could result in an endless loop.

    You're using C#? So you should be familiar with the do...while method of looping?

    Find.Execute returns a boolean true if Find was successful, otherwise false. So something along these lines:

    bool found = false;
    do
    {
      //Find stuff here
      found = wordApp.Selection.Find.Execute(//params);
      if (found)
        { //other stuff here }

    {
    while (found)

    However, I'd also change from using Selection.Find to Range.Find as this gives you more control over what's actually being searched. I can set the search range to specifically be "from the end of the last found to the end of the document". So more like this:

    Word.Range rngSearchContent = Document.Content;
    Word.Range rngSearchRange = rngSearchContent.Duplicate;
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    do
    {
      //Find para objects
      found = rngSearchRange.Find.Execute(//params)
      if (found)
      {
        //do found things
        //reset the range to be searched
        rngSearchRange.Collapse(ref oCollapseEnd);
        rngSearchRange.End = rngSearchContent.End;
      }
    } while (found)


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, January 15, 2013 9:11 AM
    Moderator
  • Hi hurrem

    The "?": It's not possible that you can pass the Execute method a string. You notice that all the other parameters have "ref" in front of them? That's because they're all optional. So C# requires you to pass them as ref objectDataType.

    <<

    The problem is on the line:

    nextChar1 = wordApp.Selection.Characters[2].Text;

    it throws an exception with some russian text, which means something like "The requested object doesn't exsists">>

    You're querying the Selection - that's the equivalent of what is actually SELECTED in the document. If the Selection contains only one character or is a blinking insertion point, then there is no second character, which explains the error message.

    Is your intention to look at the character following the last character of what was "found"? In that case, look at the Selection.MoveEnd method.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 17, 2013 12:31 PM
    Moderator

All replies

  • You don't show us all the code, so it's impossible to know... If you want Find.Execute to run more than once you need to have it in a loop. Do you? If yes, you need to show us the loop.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, January 15, 2013 6:53 AM
    Moderator
  • no, I don't have the loop...

    How to make a loop to search until the end of the document?

    Tuesday, January 15, 2013 8:22 AM
  • Hi hurrem

    First, change the wrap argument to correspond to Word.WdFindWrap.wdFindStop. You have it set to wdFindContinue which could result in an endless loop.

    You're using C#? So you should be familiar with the do...while method of looping?

    Find.Execute returns a boolean true if Find was successful, otherwise false. So something along these lines:

    bool found = false;
    do
    {
      //Find stuff here
      found = wordApp.Selection.Find.Execute(//params);
      if (found)
        { //other stuff here }

    {
    while (found)

    However, I'd also change from using Selection.Find to Range.Find as this gives you more control over what's actually being searched. I can set the search range to specifically be "from the end of the last found to the end of the document". So more like this:

    Word.Range rngSearchContent = Document.Content;
    Word.Range rngSearchRange = rngSearchContent.Duplicate;
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    do
    {
      //Find para objects
      found = rngSearchRange.Find.Execute(//params)
      if (found)
      {
        //do found things
        //reset the range to be searched
        rngSearchRange.Collapse(ref oCollapseEnd);
        rngSearchRange.End = rngSearchContent.End;
      }
    } while (found)


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, January 15, 2013 9:11 AM
    Moderator
  • What's the exception message? What line of code is triggering it?

    What's the ? mark doing in the following statement taken from your code sample? Could that be the cause of the problem?

     wordApp.Selection.Find.Execute(ref findLetter,
                                                                   
    ref matchCase, ref matchWholeWord,
                                                                   
    ref matchWildCards, ref matchSoundsLike,
                                                                   
    ref nmatchAllWordForms, ref forwardIY,
                                                                   
    ref wrap, ref format, "?",
                                                                   
    ref replaceYI, ref matchKashida,
                                                                   
    ref matchDiacritics, ref matchAlefHamza,
                                                                   
    ref matchControl);


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 16, 2013 4:26 PM
    Moderator
  • The problem is on the line:

    nextChar1 = wordApp.Selection.Characters[2].Text;

    it throws an exception with some russian text, which means something like "The requested object doesn't exsists"

    The "?" is just a string. Here I'm getting a Range, within a range, so that's the problem..

    Wednesday, January 16, 2013 7:21 PM
  • Hi hurrem

    The "?": It's not possible that you can pass the Execute method a string. You notice that all the other parameters have "ref" in front of them? That's because they're all optional. So C# requires you to pass them as ref objectDataType.

    <<

    The problem is on the line:

    nextChar1 = wordApp.Selection.Characters[2].Text;

    it throws an exception with some russian text, which means something like "The requested object doesn't exsists">>

    You're querying the Selection - that's the equivalent of what is actually SELECTED in the document. If the Selection contains only one character or is a blinking insertion point, then there is no second character, which explains the error message.

    Is your intention to look at the character following the last character of what was "found"? In that case, look at the Selection.MoveEnd method.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 17, 2013 12:31 PM
    Moderator
  • Yes,

    My intention is to look at the character following the last character of what was "found"? In that case, look at the Selection.MoveEnd method.

    Saturday, January 19, 2013 1:31 PM