Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Copying highlighted text to another document retaining the paragraph     formatting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Raj - 19 Dec 2007 06:31 GMT
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
Helmut Weber - 19 Dec 2007 11:19 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.