0

I need some help with my code, what I'm trying to do its to copy a text from 1 word and paste it in a new one without "line spaces", with my code it works but.... I lost all the format of the text and I want to keep it, is there any possibility or an alternative to do what I want?

Word.Range rng = Globals.ThisAddIn.Application.ActiveDocument.Content;

rng.Copy();

string TPaste = Clipboard.GetText();

TPaste = TPaste.Replace("\r\n", " ");
TPaste = TPaste.Replace("  ", " ");

object start = 0;
object end = 0;

rng = Protocol.Range(ref start, ref end);

rng.Text = TPaste;
eastian
  • 11
  • 1
  • Does this answer your question? [C# How can I paste formatted text from clipboard to the RichTextBox](https://stackoverflow.com/questions/9749141/c-sharp-how-can-i-paste-formatted-text-from-clipboard-to-the-richtextbox) – Franz Gleichmann Jun 23 '21 at 18:49
  • Thanks por your help, I tried all of the answers but no one works... At the moment Im trying to paste all the content at the new word doc and trying to use the find and replace tool. – eastian Jun 23 '21 at 23:41

1 Answers1

1

Ok, so finally I make it works! Here is the solution, what I did was copy al the content and paste it with the original format, them at the new word doc I use the tool "find and replace" and with that I solve my problem!

Word.Range rng = Globals.ThisAddIn.Application.ActiveDocument.Content;

        rng.Copy();

        Protocol.Content.Paste();

        Word.Find findObject = Protocol.Application.Selection.Find;
        findObject.ClearFormatting();
        findObject.Text = "^p";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = " ";

        object replaceAll = Word.WdReplace.wdReplaceAll;

        findObject.Execute(Replace: replaceAll);

        findObject.ClearFormatting();
        findObject.Text = "  ";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = " ";

        findObject.Execute(Replace: replaceAll);
eastian
  • 11
  • 1