View Single Post
 
Old 04-28-2023, 02:08 PM
tanko tanko is offline Windows 10 Office 2016
Novice
 
Join Date: Jan 2021
Posts: 17
tanko is on a distinguished road
Default Change style of portion of paragraph

Hi, I've got a simple macro that I want to use to find all paragraphs of a particular style, and then change a portion of that paragraph to a new style. The purpose is to tag the first portion as Heading 2 so it will show up in the TOC without the rest of the body of that paragraph. When I manually set the paragraph's style to my custom "Level 2" style, I can manually select the caption of that paragraph and change it to "Heading 2." Only the selected portion, the caption, will be changed. However, when I run this macro, it changes the style for the entire paragraph. How can I make Word apply the style only to the first sentence, which is the caption? I've tried using para.range.Sentence(1).select, and that doesn't work either.

For example, the following paragraph with Level 2 style:
Quote:
1.1 Proxies. At all meetings of Members, a Member may vote in person or by proxy executed in writing by the Member. Such proxy shall be filed with the Manager before or at the time of the meeting. No proxy shall be valid after eleven (11) months from the date of its execution, unless otherwise provided in the proxy.
Becomes the following, all of which is Heading 2:
Quote:
1.1 Proxies. At all meetings of Members, a Member may vote in person or by proxy executed in writing by the Member. Such proxy shall be filed with the Manager before or at the time of the meeting. No proxy shall be valid after eleven (11) months from the date of its execution, unless otherwise provided in the proxy.
Code:
Sub ChangeCaptionStyle()
    Dim para        As Word.Paragraph

    For Each para In ActiveDocument.Paragraphs
        paracheck = para.Range.Sentences(1)
        If para.Style = "Level 2" Then
            para.Range.Sentences(1).Select
            Selection.Range.Style = "Heading 2"
        End If
    Next para

    Set para = Nothing
End Sub
Reply With Quote