复制word文档中所有突出显示的文本并粘贴到另一个文档

copy all highlighted text in a word document and paste it to another document

我非常接近完成这项任务。以下 vba 代码将文本复制到新的 Word 文档中。但我希望新文档中的文本保持原始文档中的突出显示颜色。

非常感谢您的帮助。

Sub CopyHighlightsToOtherDoc()
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim r As Range
Set ThisDoc = ActiveDocument
Set r = ThisDoc.Range
Set ThatDoc = Documents.Add
With r.Find
   .Text = ""
   .Highlight = True
   Do While .Execute(Forward:=True) = True
      ThatDoc.Range.InsertAfter r.Text & vbCrLf
      r.Collapse 0
   Loop
End With
End Sub

使用 Range.FormattedText 属性 获取文本和格式。

语法是ThisRange.FormattedText = ThatRange.FormattedText

编辑:

Sub CopyHighlightsToOtherDoc()
    Dim ThisDoc As Document
    Dim ThatDoc As Document
    Dim r As Range
    Set ThisDoc = ActiveDocument
    Set r = ThisDoc.Range
    Set ThatDoc = Documents.Add
    With r
        With .Find
            .Text = ""
            .Highlight = True
        End With
        Do While .Find.Execute(Forward:=True) = True
            ThatDoc.Range.Characters.Last.FormattedText = .FormattedText
            ThatDoc.Range.InsertParagraphAfter
            .Collapse 0
        Loop
    End With
End Sub