简体   繁体   English

C# VSTO,在不丢失格式的情况下编辑剪贴板中的文本

[英]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?我的代码需要一些帮助,我正在尝试从 1 个单词复制文本并将其粘贴到一个没有“行空格”的新文本中,使用我的代码它可以工作,但是......我丢失了所有文本的格式,我想保留它,有没有可能或替代方法来做我想做的事?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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