none
Find text in shapes,charts...etc. RRS feed

  • Question

  • Hi everyone,

    how can i find a text in shape? like Microsoft Word Find operation that can find text in every location

    Saturday, March 19, 2011 12:04 PM

Answers

  • Hi Everyone,

    i solve this , here is my code:

    int shapeindex;

       public void FindInTexbox(string TextFind, bool MatchWholeWord)
            {
                try
                {
                    
                      object findText = TextFind;
                      object MatchWhw = MatchWholeWord;
                      Word.Shapes Wshapes = Application.ActiveDocument.Shapes;
                      Word.Shape Wshape;
                      if (shapeindex >= Wshapes.Count)
                      {
                          shapeindex = 1;
                      }
                      while (shapeindex <= Wshapes.Count)
                      {
                          Wshape = Wshapes[shapeindex];
                          if (Wshape.TextFrame.TextRange.Words.Count!= 0)
                          {
                              
                              Wshape.TextFrame.TextRange.Find.Text = TextFind;
                              Wshape.TextFrame.TextRange.Find.ClearFormatting();
     
                              if (Wshape.TextFrame.TextRange.Find.Execute(ref findText,
                        ref missing, ref MatchWhw, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing) == false)
                              {
                                  shapeindex++;
     
                                
                              }
                              else
                              {
                                  Wshape.TextFrame.TextRange.Select();
                                  shapeindex++;
                                  break;
                              }
                          }
                          else
                          {
                              shapeindex++;
                          }
                      }
     
                   
     
                    
                    
       
                         
                }
                catch (Exception ex)
                { 
                    MessageBox.Show(ex.Message);
                }
            }
    but when we found a word in a shape , its select all TextRange , it's not so good but enough for me now
    Wednesday, March 23, 2011 9:18 AM

All replies

  • Hi Adnan,

    To do this, you can loop through all of the document's storyranges:

    Dim Rng As Range
    For Each Rng In ActiveDocument.StoryRanges
      With Rng.Find
        'insert your find code here
      End With
    Next

    Alternatively, you could restrict the Find to shape objects, using code like:

    Dim shp As Shape
    With ActiveDocument
      For Each shp In .Shapes
        If shp.TextFrame.HasText Then
          With shp.TextFrame.TextRange.Find
            'insert your find code here
          End With
        End If
      Next
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Sunday, March 20, 2011 7:48 AM
  • Hi Paul,

     

    can we act like "Find Next" with these codes?

    Wednesday, March 23, 2011 6:30 AM
  • Hi Adnan,

    Since the code already loops through the shapes collection, that takes care of the basics. Within the code, though, you could use a messagebox to ask the user whether to continue or exit. For example:

    Dim shp As Shape
    With ActiveDocument
      For Each shp In .Shapes
        If shp.TextFrame.HasText Then
          With shp.TextFrame.TextRange.Find
            'insert your find code here
          End With
          If MsgBox("Continue?", vbYesNo) = vbNo Then Exit Sub
        End If
      Next
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, March 23, 2011 8:35 AM
  • Hi Everyone,

    i solve this , here is my code:

    int shapeindex;

       public void FindInTexbox(string TextFind, bool MatchWholeWord)
            {
                try
                {
                    
                      object findText = TextFind;
                      object MatchWhw = MatchWholeWord;
                      Word.Shapes Wshapes = Application.ActiveDocument.Shapes;
                      Word.Shape Wshape;
                      if (shapeindex >= Wshapes.Count)
                      {
                          shapeindex = 1;
                      }
                      while (shapeindex <= Wshapes.Count)
                      {
                          Wshape = Wshapes[shapeindex];
                          if (Wshape.TextFrame.TextRange.Words.Count!= 0)
                          {
                              
                              Wshape.TextFrame.TextRange.Find.Text = TextFind;
                              Wshape.TextFrame.TextRange.Find.ClearFormatting();
     
                              if (Wshape.TextFrame.TextRange.Find.Execute(ref findText,
                        ref missing, ref MatchWhw, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing) == false)
                              {
                                  shapeindex++;
     
                                
                              }
                              else
                              {
                                  Wshape.TextFrame.TextRange.Select();
                                  shapeindex++;
                                  break;
                              }
                          }
                          else
                          {
                              shapeindex++;
                          }
                      }
     
                   
     
                    
                    
       
                         
                }
                catch (Exception ex)
                { 
                    MessageBox.Show(ex.Message);
                }
            }
    but when we found a word in a shape , its select all TextRange , it's not so good but enough for me now
    Wednesday, March 23, 2011 9:18 AM