Copying highlighted text to another document retaining the paragraphformatting

R

Raj

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
 
H

Helmut Weber

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
 
R

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

Thanks. It worked.

Regards,
Raj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top