none
Find Next in Shapes

    Question

  • Hi everyone,

    i've used these codes for (Find Next) Method in a Word Add-in:

       public void FindText(string TextFind, bool MatchWholeWord)
            {
                try
                {
                    object find = TextFind;
                    object match = MatchWholeWord;
                    Application.Selection.Find.ClearFormatting();
                    if (!Application.Selection.Find.Execute(ref find,
                        ref missing, ref match, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    
                    {
                       
                        DialogResult result;

                        // Displays the MessageBox.

                        result = MessageBox.Show("Word not found""Start search from begining?"MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button2,MessageBoxOptions.RightAlign);

                        if (result == System.Windows.Forms.DialogResult.Yes)
                        {

                            Word.Range rng = this.Application.ActiveDocument.Range(0, 0);



                            rng.Select();

                           

                        }


                       
                    } 
                }

                catch (Exception ex)
                { MessageBox.Show(ex.Message); }

    but it dosen't find words in a Shapes , how can i find words in a shapes?

    Saturday, March 05, 2011 9:49 AM

All replies

  • Hi Adnan,

    Thanks for posting in the MSDN Forum.

    As far as I can see, you use “!Application.Selection.Find.Execute” in you sentence. Its mean is if application can no find anything in the document. Next thing is “Word.Rang rng = this.Application.ActiveDocument.Range(0,0)”. Its mean is the range you declared is a specific range, it isn’t the range which you want to search.

    So, please try this snippet.

    if (Application.Selection.Find.Execute(ref find,ref missing, ref match, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing))                
          {                    
            DialogResult result;           
            // Displays the MessageBox.           
            result = MessageBox.Show("Word not found", "Start search from begining?", MessageBoxButtons.YesNo, MessageBoxIcon.None,MessageBoxDefaultButton.Button2,MessageBoxOptions.RightAlign);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {             
              Word.Range rng = this.Application.Selection.Range             
              rng.Select();                       
            } 
    

    I hope it helps.

    Have a great day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 07, 2011 2:56 AM
  • Hi Tom.

    if (!Application.Selection.Find.Execute(ref find,
                        ref missing, ref match, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))

    In "IF" condition area we find word if it's False , but when this condition throw "True" value it means  Word Not Found and Then :

    {
                       
                        DialogResult result;

                        // Displays the MessageBox.

                        result = MessageBox.Show("Word not found""Start search from begining?"MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button2,MessageBoxOptions.RightAlign);

                        if (result == System.Windows.Forms.DialogResult.Yes)
                        {

                            Word.Range rng = this.Application.ActiveDocument.Range(0, 0);



                            rng.Select();

                           

                        }


                       
                    } 

     this section means if you Click "YES" , selection range is set to (0,0) and next you try finding next word ,it's start from begining of a document

    Anyway...my problem still it doesn't find any word in a Shapes!

     

    Monday, March 07, 2011 8:51 AM
  • Hi Adnan,

    When you executed Application.Selection.Find.Execute method, if the method find the word you can get the range of your word from Application.Selection.Rang.

    I hope it helps.

    Have a good day,


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 08, 2011 2:59 AM
  • Text in Shapes is in the TextFrame Story - to find it, that is where you must search.
     
    I don't know C#, but in VBA you would do something along the lines of ...
     
        document_ref.StoryRanges(wdTextFrameStory).Find.whatever
     
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, March 09, 2011 11:55 AM
  • Hi Tony.

    Thanks for your reply its very helpful

    in C# the StoryRanges is:

     

     Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdTextFrameStory].Find

    but it doesn't find anything

    this is my method can you review it?

     

      public void FindInTexbox(string TextFind, bool MatchWholeWord)
            {
      try
        {
              
            
                    object find = TextFind;
                    object match = MatchWholeWord;
          Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdTextFrameStory].Find.ClearFormatting();
     if (!Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdTextFrameStory].Find.Execute(ref find,
          ref missing, ref match, ref missing, ref missing, ref missing, ref missing,
          ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
          ref missing, ref missing))
         {
                        DialogResult result;
                        // Displays the MessageBox.
        result = MessageBox.Show("Start From Beginning?""Word Not Found"MessageBoxButtons.YesNo,
           MessageBoxIcon.None, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign);
     
                        if (result == System.Windows.Forms.DialogResult.Yes)
                        {
                            Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
                            rng.Select();
                        }
     
         }
        }
     
     catch (Exception ex)
    MessageBox.Show(ex.Message); }
            }

     

    Thursday, March 10, 2011 2:22 PM
  • As best I can tell, the code looks good - if you get the message box, the Find has Found nothing.
     
    A couple of things ...
     
        How have you created your text in shapes? I am making assumptions about it.
       
        For debugging, can you check the StoryRange Text just to confirm it does contain what you think it contains?
     
        The ClearFormatting achieves nothing - the Range Find object is transient and the next statement, the Execute, will cause a new Find object to be instantiated (with formatting cleared)
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, March 10, 2011 6:21 PM
  • Hi Tony,

    Yes i have text in shapes and i commented ClearFormatting and debuging again but nothing changed...

     

    so..if you want to search a word in a shapes , what will you do else?

    Sunday, March 13, 2011 6:58 AM
  • If the text is in a TextBox in the Shape - which I am presuming it is, but you are not confirming - then that Story is where you should find it. Maybe there's some facet of C# causing the problem, but as I don't know the language I can't say.
     
    Is your code seeing the text in that Story, and the Find not finding it, or are you not seeing the text to begin with?
     

    Enjoy,
    Tony
    www.WordArticles.com
    Sunday, March 13, 2011 11:24 AM

  • Hi Tony,

    I found something here : http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/36cebc60-7c7e-494c-ad2d-0dcebce5a706

    i used it but some feature are not working ,

     for example it's a textbox==>   |Book Book|

    this method found and select first "Book" always :

    by the way another problem that i have , sometimes if i have more than one texbox , some of textboxes not found!

      public void FindInTexbox(string TextFind, bool MatchWholeWord)
            {
                try
                {
                   // Application.Selection.WholeStory();
                    object find = TextFind;
                    object match = MatchWholeWord;
                    if (myStoryRange == null)
                    {
                        myStoryRange = Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdTextFrameStory];
                    }
                        myStoryRange.Find.ClearFormatting();
                        myStoryRange.Find.Text = TextFind;
                        myStoryRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
                        if (myStoryRange.Find.Execute(ref find, ref missing, ref match,
                           ref missing, ref missing, ref missing, ref missing,
                           ref missing, ref missing, ref missing, ref missing,
                           ref missing, ref missing, ref missing, ref missing))
                        {
                            myStoryRange.Select();
                        }
                        myStoryRange = myStoryRange.NextStoryRange; 
                }
                catch (Exception ex)
                { 
                    MessageBox.Show(ex.Message);
                }
            }
    Thursday, March 17, 2011 10:19 AM