Search code examples
.netms-wordinteroprangeparagraphs

Edit a word paragraph using C#.net


am working on a word document Programmatically using C#.net, Interop.Word, I have paragraphs in this document starting and ending with a "#".

Example:

Once upon a time, there was a #little girl who lived in a village near the forest. Whenever she went out, the little girl wore a red riding cloak, so everyone in the village called her Little Red Riding Hood.#

One morning, Little Red Riding Hood #asked her mother if she could go to visit her grandmother as it had been awhile since they'd seen each other.#

"That's a good idea," her mother said. #So they packed a nice basket for Little Red Riding Hood to take to her grandmother.#

Now between all # I need the text be bold


Solution

  • Fixed that.

            oWord.Selection.HomeKey( Microsoft.Office.Interop.Word.WdUnits.wdStory );
            oWord.Selection.ClearFormatting();
    
            bool done = false;
            while ( !done )
            {
               object txt = "#*#";
               object oFalse = false;
               object oTrue = true;
               object wdWrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;
               oWord.Selection.Find.Execute( ref txt, ref oFalse, ref oFalse, ref oTrue, ref oFalse, ref oFalse, ref oTrue, ref wdWrap, ref oFalse );
               if ( oWord.Selection.Find.Found == false )
               {
                  done = true;
               }
               else
               {
                  oWord.Selection.Font.Bold = 1;
                  oWord.Selection.Text = oWord.Selection.Text.Substring( 1, oWord.Selection.Text.Length - 2 );
                  oWord.Selection.MoveRight( Microsoft.Office.Interop.Word.WdUnits.wdCharacter );
               }
            }