vb.net如何改变字符串样式粘贴到Word

需求如下:
首先定义一串字符串
Dim str as string="这是一串文字"

如何改变文字颜色?比如颜色改成红色后将字符串复制到系统剪贴板中
或者字符串复制到剪贴板后再改
Clipboard.SetText(str)

打开Word后使用Ctrl+V实现粘贴出红色的"这是一串文字"

需求
VBA:如何在粘贴前从剪贴板修改文本的颜色


解决办法
使用下面的代码即可:

Sub PasteUnformattedText()


Selection.EndKey Unit:=wdLine

Selection.Font.Color = 12611584

MyText = " "
Selection.TypeText (MyText)
Selection.PasteSpecial DataType:=wdPasteText

End Sub

参考:
https://stackoverflow.com/questions/30189566/vb-2010-changing-the-color-of-a-word-in-a-textbox-every-copy-of-the-word

建议可以直接代码操作,生成Word。引入spire.doc.dll,参考下面的代码来操作:

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Imports System.IO

Namespace CreateWord_Doc
    Class Program
        Private Shared Sub Main(args As String())
            '加载Word文档m,添加文字到段落
            Dim doc As New Document()
            doc.LoadFromFile("test.docx")
            Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(0)
            Dim text As TextRange = paragraph.AppendText("这是一串文字")
            text.CharacterFormat.TextColor = Color.Red


            '新建Word,添加文字到段落
            'Document doc = new Document();
            'Section section = doc.AddSection();
            'Paragraph paragraph = section.AddParagraph();
            'TextRange text = paragraph.AppendText("这是一串文字");
            'text.CharacterFormat.TextColor = Color.Red;

            '保存文档
            doc.SaveToFile("result.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("result.docx")
        End Sub
    End Class
End Namespace

文字添加效果:

img

str.Color = System.Drawing.Color.FromName("Lime")
采用这种方式,如有帮助,请采纳

vb.net字符串不带样式,你调用系统剪贴板就行