none
how to select an specific word in word docs ? RRS feed

  • Question

  • Hi

    i have a csharp app which work with word docs. i want to select a word in the word docs base on my criteria. then replace it with a table with some other data.

    for example, if my app found this word : "#table#", then replace it with a table.

    can anybdoy help me ?


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    Tuesday, June 21, 2011 5:45 PM

Answers

  • Hi again

    I solved my problem!

    i've wrote this method to find a range which start and end base on my criteria, then i everyThing in founded range.

     

    private Microsoft.Office.Interop.Word.Range FindRangeByWord(Microsoft.Office.Interop.Word.Document wordDoc, string wordSearch)
        {
          Microsoft.Office.Interop.Word.Range range = null;
          object startPosition = 0;
          object endPosition = (object)wordDoc.Characters.Count;
    
          range = wordDoc.Range(ref startPosition, ref endPosition);
          string text = range.Text;
    
          // Select All Instances of of the word
          int startIndex = text.IndexOf(wordSearch);
          int endIndex = startIndex + wordSearch.Length;
    
          // convert startPosition & endPosition from startIndex & endIndex
          startPosition = (object)startIndex;
          endPosition = (object)endIndex;
    
          range = wordDoc.Range(ref startPosition, ref endPosition);
          return range;
        }
    



    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    • Marked as answer by Hamed_1983 Tuesday, June 21, 2011 7:37 PM
    Tuesday, June 21, 2011 7:37 PM