Log in

View Full Version : Format paragraphs that start with certain words?



Phreaddy
04-24-2018, 08:23 AM
Hi, all,

Using Word 2016/365, my team and I write about 50 monthly reports with headings that always start with the day, month and date, like "Monday, January 1." That's the whole paragraph.

Anyone know how I could code VBA to, for example, take every paragraph that starts with a day of the week, turn it bold and turn on the "keep with next" feature? I could probably figure out the formatting by recording a macro formatting text, and seeing the code that it came up with. But I can't figure out how to code searching for days of the week at the start of a paragraph.

TIA for any help!

Phreaddy

gmaxey
04-24-2018, 02:03 PM
Simplistic, but should get the job done:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 4/24/2018
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Range.Paragraphs
If Trim(oPar.Range.Words(1)) Like "[MTWFS]*day" Then
oPar.Range.Font.Bold = True
oPar.KeepWithNext = True
End If
Next
lbl_Exit:
Exit Sub
End Sub

Phreaddy
04-24-2018, 02:30 PM
Thanks -- I'll give that a try!

macropod
04-24-2018, 04:45 PM
Unless you have other paragraphs with the dates at their ends, you don't actually need a macro. Simply use a wildcard Find/Replace, where:
Find = <[MTWFS][ondayueshrit]{2,7}, [JFMASOND][anuryebchpilgstmov]{2,8} [0-9]{1,2}.^13
Replace = ^&
and you set the Replace character and paragraph formats with the bold and Keep With Next attributes, respectively.

That said, I'd recommend defining a paragraph Style with the bold and Keep With Next attributes and use that as the replacement Style. You could, of course, record the above as a macro.

Phreaddy
04-25-2018, 06:22 AM
Thanks, folks. Gmaxey, it worked like a charm. Macropod, I'll keep a link to yours, too, in case I ever have any problems.

macropod
04-25-2018, 02:50 PM
Cross-posted at: http://www.msofficeforums.com/word-vba/38879-reformat-paragraph-starts-certain-words.html

Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

FWIW, Greg's macro will also process paragraphs starting with 'Today' or 'Someday', for example.

gmaxey
04-25-2018, 07:50 PM
Paul,

True, and that was why I led off with "Simplistic". But, considering his description, I thought it might work for him. We could change to::


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 4/24/2018
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Range.Paragraphs
If Trim(oPar.Range.Words(1)) Like "[MTWFS]*[inrs]day" Then
oPar.Range.Font.Bold = True
oPar.KeepWithNext = True
End If
Next
lbl_Exit:
Exit Sub
End Sub

macropod
04-25-2018, 08:15 PM
Or, for something far quicker:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Text = "<[MTWFS][ondayueshrit]{2,7}, [JFMASOND][anuryebchpilgstmov]{2,8} [0-9]{1,2}.^13"
With .Replacement
.Text = "^&"
.ClearFormatting
.ParagraphFormat.KeepWithNext = True
.Style = "Strong"
End With
.Format = True
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub

gmaxey
04-26-2018, 04:08 AM
An expression of a truly bright mind at work.