c#, word addin, programmary replace formatted text in paragraphs (links, content) RRS feed

  • Question

  • Hello,

    I try to create word addin.

    How i can programmary replace text in paragraphs? I need to replace: "\r" to "\t\r" in line.

    I try: 

    1) Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[nr].Range.Text = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[nr].Range.Text.Replace("\r", "\t\r");

    its good only for text, if i have links or content in paragraphs its fail :(


                    //        //Globals.ThisAddIn.Application.Selection.Text = "\t"



    Globals.ThisAddIn.Application.Selection.Find.Execute("\r", Wrap: Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue);

                Word.Find findObject = Application.Selection.Find;
             //   findObject.ClearFormatting();
                findObject.Text = "\r";
             //   findObject.Replacement.ClearFormatting();
                findObject.Replacement.Text = "\t" + Environment.NewLine;

                object replaceAll = Word.WdReplace.wdReplaceOne;
                findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref replaceAll, ref missing, ref missing, ref missing, ref missing);


    Wednesday, November 5, 2014 3:57 PM


  • Hi Drrq00z,

    Based on the description and code, you want to replace the "\r" to "\t\r". As far as I test, we can replace it successully when a paragrapy contain hyperlinks, but all hyperlinks would be removed. Did this you mean fail?

    And as far as I know, the "\n" in document mean an paragrapy character. If you want to insert a tab character before the paragraphy character, we can find paragraphy and then insert the tab character. Here is a sample for your reference:

                Globals.ThisAddIn.Application.Selection.Find.Text = "^p";
                Globals.ThisAddIn.Application.Selection.Find.Replacement.Text = "";
                Globals.ThisAddIn.Application.Selection.Find.Forward = false;
                Globals.ThisAddIn.Application.Selection.Find.Wrap = WdFindWrap.wdFindContinue;
                Globals.ThisAddIn.Application.Selection.Find.Format = false;
                Globals.ThisAddIn.Application.Selection.Find.MatchCase = false;
                Globals.ThisAddIn.Application.Selection.Find.MatchWholeWord = false;
                Globals.ThisAddIn.Application.Selection.Find.MatchWildcards = false;
                Globals.ThisAddIn.Application.Selection.Find.MatchSoundsLike = false;
                Globals.ThisAddIn.Application.Selection.Find.MatchAllWordForms = false;
                         while (Globals.ThisAddIn.Application.Selection.Find.Execute())


    Regards & Fei

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 7, 2014 6:37 AM