简体   繁体   中英

C# VSTO, edit text from the clipboard without losing format

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;

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM