When reading a document, I highlight important/interesting text. I
later extract these highlighted portions to another document using the
following code (from the Web):
Sub ExtractHighlightedText
Dim oDoc As Document
Dim s As String
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = ""
.Highlight = True
Do While .Execute
s = s & Selection.Text & vbCrLf
Loop
End With
End With
Set oDoc = Documents.Add
oDoc.Range.InsertAfter s
End sub
However, the text is copied without any formatting. I want the
original formatting to be retained. How can I do this?
Thanks in advance for the help.
Regards,
Raj
Hi Raj,
you need another approach, like that:
Sub Testx()
Dim oDc1 As Document ' source
Dim oDc2 As Document ' target
Dim rDc1 As Range
Dim rDc2 As Range
' ----------------
Set oDc1 = Documents("source.doc")
Set oDc2 = Documents("target.doc")
Set rDc1 = oDc1.Range
Set rDc2 = oDc2.Range
' ----------------
With rDc1.Find
.Highlight = True
While .Execute
rDc2.Collapse Direction:=wdCollapseEnd
rDc2.FormattedText = rDc1.FormattedText
rDc2.InsertAfter vbCrLf
Wend
End With
End Sub
--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Vista Small Business, Office XP
Raj - 19 Dec 2007 13:08 GMT
> Hi Raj,
>
[quoted text clipped - 27 lines]
>
> Vista Small Business, Office XP
Thanks. It worked.
Regards,
Raj