Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-28-2023, 02:08 PM
tanko tanko is offline Change style of portion of paragraph Windows 10 Change style of portion of paragraph Office 2016
Novice
Change style of portion of paragraph
 
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
  #2  
Old 04-30-2023, 02:12 PM
Charles Kenyon Charles Kenyon is offline Change style of portion of paragraph Windows 11 Change style of portion of paragraph Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Ideally, you would be applying the heading style to an entire paragraph, not just part. Then use a Style Separator to separate your heading, that you want in the TOC, frcom the run-on part that you want to be part of the same logical paragraph.
Reply With Quote
  #3  
Old 05-04-2023, 04:17 PM
tanko tanko is offline Change style of portion of paragraph Windows 10 Change style of portion of paragraph Office 2016
Novice
Change style of portion of paragraph
 
Join Date: Jan 2021
Posts: 17
tanko is on a distinguished road
Default

Thanks, Charles. Unfortunately, the TOC-referenced style here must be a part of a larger paragraph, since that is the near-universal preference in modern contracts. Also unfortunately, I can't stand style separators and the lawyers I work with hate them worse because hidden characters like that they make editing difficult (that is, selecting the entire paragraph with triple click only selects the text on that side of the separator, which contradicts the typically understood behavior).

The only solutions I've found to this are (1) enabling linked styles and then applying the appropriate Heading style to the caption of the section, which is pretty cumbersome, or (2) using TOC entries, which is extremely cumbersome. I only ever use method (1).

Until now, I've been manually creating my styles and then manually applying the appropriate heading to the caption. But I'd like to do that programmatically, but VBA is not cooperating in applying the heading style to just a portion of the paragraph as a linked style, hence my question.

Any ideas>
Reply With Quote
  #4  
Old 05-05-2023, 11:52 AM
Italophile Italophile is online now Change style of portion of paragraph Windows 11 Change style of portion of paragraph Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Although you may think linked styles to be cumbersome, this is precisely the use case that they were designed for.

As their name suggests, linked styles consist of two separate styles - a paragraph style and a character style with the same name plus the suffix " Char". When a linked style is applied via the UI Word decides which style to apply dependent on context.

When applied via VBA it is up to you to decide which style to apply.

To apply a heading style to the entire paragraph you can either use the name:
Code:
Selection.Range.Style = "Heading 2"
or, especially valuable if the user's OS language isn't English, the enum:
Code:
Selection.Range.Style = wdStyleHeading2
To apply the character style you can either use the name directly:
Code:
Selection.Range.Style = "Heading 2 Char"
or construct the name:
Code:
Selection.Range.Style = ActiveDocument.Styles(wdStyleHeading2).NameLocal & " Char"
Reply With Quote
  #5  
Old 05-05-2023, 01:37 PM
tanko tanko is offline Change style of portion of paragraph Windows 10 Change style of portion of paragraph Office 2016
Novice
Change style of portion of paragraph
 
Join Date: Jan 2021
Posts: 17
tanko is on a distinguished road
Default

Bingo, that's it, thanks! Interesting how Word decides to apply char or para style based on user behavior. Makes sense.

And to be clear, I don't find the linked styles cumbersome, I find manually applying two styles to each paragraph to be cumbersome. My code now eliminates at least one step of that.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change style of portion of paragraph Word 2007: Unable to change character style, when using a linked Char/Para style format Last Chance Word 3 06-09-2021 12:52 PM
Why does paragraph inherit style of the following paragraph? WADEVCAMP Word VBA 2 04-08-2019 02:13 PM
Change style of portion of paragraph How to globally change the formatting of a bullet style to another style? ravl13 Word 5 03-10-2013 05:04 PM
Don't indent the fist line of the first paragraph after a style change Inspirement Word 1 04-08-2012 05:30 AM
Character style stripped while applying paragraph style sams_gates Word 0 08-29-2009 02:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:57 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft