Results 1 to 5 of 5
  1. #1
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Extracting bold text via macro! (Word 2000>)

    Could someone help me to create a macro that will extract all boled sentences from a loooong document, and paste the sentences into a new blank document. Basically I want to conpile ALL bolded statements into a new document.
    Tx
    Regards,
    Rudi

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Extracting bold text via macro! (Word 2000>)

    This can be tricky, for a sentence will only be considered bold if it is bold in its entirety. If one word in the sentence is not bold, the sentence as a whole is not considered to be bold. The following macro looks at sentences as a whole:

    Sub CopyBold()
    Dim docCur As Document
    Dim docNew As Document
    Dim snt As Range
    Dim rng As Range

    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    Set docCur = ActiveDocument
    Set docNew = Documents.Add
    For Each snt In docCur.Sentences
    If snt.Bold = True Then
    snt.Copy
    Set rng = docNew.Range
    rng.Collapse wdCollapseEnd
    rng.Paste
    rng.InsertParagraphAfter
    End If
    Next snt

    ExitHandler:
    Application.ScreenUpdating = True
    Set snt = Nothing
    Set docNew = Nothing
    Set docCur = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  3. #3
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extracting bold text via macro! (Word 2000>)

    Wow. This works well....very well! Tx.
    The document will only have bolded sentences. There will not be the case that one word in the sentence is not bold.

    I must definetely top muself up on the objects in Words library. I was wondering how you were going to select the sentence in the document when in the previous thread you said that there was no keyboard shortcut to select a sentence while recording. I notive now that there is a reference to sentences in the Document Object as with : For Each snt In docCur.Sentences. I must go and play with this and see what other references there are. (IE, Paragraph, Word, Character, Line, Bookmark....)

    Thanx for the code. It gives me a starting point to expand with!
    Regards,
    Rudi

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Extracting bold text via macro! (Word 2000>)

    Note that the macro doesn't select the sentences at all. The Sentences collection is a collection of Range objects that represents the individual sentences; you can loop through the elements of this collection without actually selecting them.

  5. #5
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extracting bold text via macro! (Word 2000>)

    Yes. I noticed that. Same with Excel. Its the power that VBA (Dimmed Variables) holds. <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>
    Regards,
    Rudi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •