Place two styles in one sentence separated by a comma

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Place two styles in one sentence separated by a comma

Post by SmallFry »

In the example below, each one of these five sentences ends in a paragraph mark.

In the selection, I would like to bold the first half of the sentence, and then after the comma, italics.

The comma should remain normal style.

Air Force Technical Order Form 22, Technical Manual Change Recommendation and Reply
Air Force Form 332, Base Civil Engineer (CE) Work Request
Technical Order (TO) 00-5-15, Time Compliance Technical Order Process
TO 00-25-107, Engineering Technical Assistance Request
TO 1-1B-50, Aircraft Weight and Balance

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Place two styles in one senctence seperated by a comma

Post by StuartR »

You could apply the character styles Strong and Emphasis to the words you want to be bold and italic
StuartR


SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Place two styles in one senctence seperated by a comma

Post by SmallFry »

Hi Stuart and thank you for your time. Is it possible to do this with a macro?

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Place two styles in one senctence seperated by a comma

Post by HansV »

Try this:

Code: Select all

Sub ApplyBoldItalic()
    Dim par As Paragraph
    Dim rng As Range
    For Each par In Selection.Paragraphs
        Set rng = par.Range
        rng.MoveEndUntil Cset:=",", Count:=wdBackward
        rng.MoveEnd Count:=-1
        rng.Style = ActiveDocument.Styles(wdStyleStrong)
        Set rng = par.Range
        rng.MoveStartUntil Cset:=",", Count:=wdForward
        rng.MoveStart Count:=1
        rng.Style = ActiveDocument.Styles(wdStyleEmphasis)
    Next par
End Sub
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Place two styles in one senctence seperated by a comma

Post by SmallFry »

Hi Hans, This works great. Instead of the Selection.Paragraphs I passed a range to process this macro. All that works fine. The range is between two specific words so now it is aRng.Paragraphs

I do have one other area between two ranges that has three parts to each sentence, but I only need to bold the first section and middle with italics. The date on the end will remain as is.

AF Doctrine Document 1-2, Air Force Glossary, 11 January 2007
AFI 17-1301, Risk Management Framework (RMF) for Air Force Information Technology (IT), 2 February 2017
DoD 36-22, Vehicle Management, 22 April 2012

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Place two styles in one sentence separated by a comma

Post by HansV »

Code: Select all

Sub ApplyBoldItalic2(aRng As Range)
    Dim par As Paragraph
    Dim rng As Range
    For Each par In aRng.Paragraphs
        Set rng = par.Range
        rng.Collapse Direction:=wdCollapseStart
        rng.MoveEndUntil Cset:=","
        rng.MoveEnd Count:=-1
        rng.Style = ActiveDocument.Styles(wdStyleStrong)
        rng.Collapse Direction:=wdCollapseEnd
        rng.Move Count:=2
        rng.MoveEndUntil Cset:=","
        rng.MoveEnd Count:=-1
        rng.Style = ActiveDocument.Styles(wdStyleEmphasis)
    Next par
End Sub
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Place two styles in one sentence separated by a comma

Post by SmallFry »

Thanks Hans. One small adjustment to the rng.MoveEnd Count:

I had to change the first and last one to 0 as the last character before the comma was not getting the style applied. This middle I changed to a 1.

I removed the Count, but then the style was applied to the comma. This seems to fix it. Thanks again.

Code: Select all

Sub ApplyBoldItalic2(aRng As Range)
    Dim par As Paragraph
    Dim rng As Range
    For Each par In aRng.Paragraphs
        Set rng = par.Range
        rng.Collapse Direction:=wdCollapseStart
        rng.MoveEndUntil Cset:=","
        rng.MoveEnd Count:=0
        rng.Style = ActiveDocument.Styles(wdStyleStrong)
        rng.Collapse Direction:=wdCollapseEnd
        rng.Move Count:=1
        rng.MoveEndUntil Cset:=","
        rng.MoveEnd Count:=0
        rng.Style = ActiveDocument.Styles(wdStyleEmphasis)
    Next par
End Sub