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 / May 2008

Tip: Looking for answers? Try searching our database.

Extract character styles into new document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
caveatRob@nospam.gmail.com - 13 May 2008 19:57 GMT
Hi all,

I'm trying to extract text with particular character styles into a new
document. I've tried the following code, and it just runs an endless
loop:

Sub CollectCustomTopics()
' highlight parts of speech in a particular color then count the
number of lines in the new document!

Dim NewDoc As Document, MainDoc As Document, r As Range

If Documents.count = 0 Then Exit Sub
Set MainDoc = ActiveDocument
Set NewDoc = Documents.Add
MainDoc.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
 .Text = ""
 .Forward = True
 .Wrap = wdFindContinue
 .Format = False
 .Highlight = True

 Do While .Execute
   DoEvents

   Set r = NewDoc.Range
   r.Collapse wdCollapseEnd
   Select Case Selection.Style

       Case "Topic Level 1"

           r.InsertAfter "Topic Level 1"
           'r.ParagraphFormat.LeftIndent = 18

       Case "Topic Level 2"
           r.InsertAfter "Topic Level 2"
       Case "Topic Level 3"
           r.InsertAfter "Topic Level 3"
       Case "Topic Level 4"
           r.InsertAfter "Topic Level 4"
       Case "Topic Level 5"
           r.InsertAfter "Topic Level 5"
       Case "Topic Level 6"
           r.InsertAfter "Topic Level 6"
     End Select

     r.InsertAfter Selection.Range.FormattedText
           If Selection.Characters.Last.Text <> vbCr Then _
             r.InsertAfter vbCr

   Loop
End With
NewDoc.Activate
End Sub
Helmut Weber - 14 May 2008 16:07 GMT
Hi Rob,

at a long shot,
as setting up such styles and an according document
is too much for me right now.

Try to define the selection anew
before the end of your loop.

>Sub CollectCustomTopics()
[snip]
>With Selection.Find
>  .Highlight = True
[quoted text clipped - 4 lines]
>        Case "Topic Level 1"
>            r.InsertAfter "Topic Level 1"
[snip]
    selection.start = selection.end
    selection.end = activedocument.range.end
>    Loop
>End With
>End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
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.