none
How to Search String Position in the Word 2003 document ? RRS feed

  • Question

  • I need help, I have 2 questions.

    (1) How to respond the position of string in the Word 2003 document ?

    (2) How to remove a paragraph, if it found the position?

     

    Thank you a lot.

     

    public void Find_String_Position_InDocument(Word.Range rng)
    {
        Int16 PosFound = 0;

        object missing = System.Reflection.Missing.Value;
        object replaceAll = Word.WdReplace.wdReplaceAll;

        rng.Find.ClearFormatting();
        rng.Find.Forward = true;
        rng.Find.Text = "find me";

        rng.Find.Execute(
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing);

        PosFound = 0;  << How to respond the String position in the document.

        MessageBox.Show("Postion found : " + PosFound.ToString());
    }   

    • Moved by Andreas Johansson Thursday, August 18, 2011 9:21 AM (From:Visual C# General)
    Thursday, August 18, 2011 9:06 AM

Answers

  • Hello,

     

    The find method selects the word searched. So, use Globals.ThisAddIn.Application.Selection.Range.Start to get the start position. To get the paragraph, i do not know an easy way, but to iterate all paragraphs and remove that which has your range.

     

    Another method could be:

     

        System.Object missing = System.Type.Missing;
        string searchedWord="";
        foreach (Microsoft.Office.Interop.Word.Paragraph para in doc.Paragraphs)
        {
         int foundPosition = para.Range.Text.IndexOf(searchedWord);
         if (foundPosition > 0)
         {
          foundPosition += para.Range.Start; // this is the location in the document;
         }
         para.Range.Delete(ref missing, ref missing); // to delete paragraph;
        }
    

     

    LE: This code is for Office 2007, but it should work for 2003 also. It uses the whole document. But you can replace doc.paragraphs with range.paragraphs if you want to search in a specific zone.


    Hope this helps,

    Silviu.


    http://www.rosoftlab.net/
    • Edited by Lupu Silviu Thursday, August 18, 2011 12:05 PM added LE
    • Marked as answer by Isaiah Walis Friday, August 19, 2011 2:02 PM
    Thursday, August 18, 2011 11:41 AM

All replies

  • Hello,

     

    The find method selects the word searched. So, use Globals.ThisAddIn.Application.Selection.Range.Start to get the start position. To get the paragraph, i do not know an easy way, but to iterate all paragraphs and remove that which has your range.

     

    Another method could be:

     

        System.Object missing = System.Type.Missing;
        string searchedWord="";
        foreach (Microsoft.Office.Interop.Word.Paragraph para in doc.Paragraphs)
        {
         int foundPosition = para.Range.Text.IndexOf(searchedWord);
         if (foundPosition > 0)
         {
          foundPosition += para.Range.Start; // this is the location in the document;
         }
         para.Range.Delete(ref missing, ref missing); // to delete paragraph;
        }
    

     

    LE: This code is for Office 2007, but it should work for 2003 also. It uses the whole document. But you can replace doc.paragraphs with range.paragraphs if you want to search in a specific zone.


    Hope this helps,

    Silviu.


    http://www.rosoftlab.net/
    • Edited by Lupu Silviu Thursday, August 18, 2011 12:05 PM added LE
    • Marked as answer by Isaiah Walis Friday, August 19, 2011 2:02 PM
    Thursday, August 18, 2011 11:41 AM
  • Very useful.

    thank you.

    Friday, August 19, 2011 2:05 PM