PDA

View Full Version : macro for detecting entire line with style applied



eggre
04-24-2013, 02:32 PM
In a nutshell, I need to detect when an entire line has style x applied and then restyle it y. I've played with string wildcards, without any luck. I hope I'm missing something obvious. Suggestions?

Example:
This entire bold line would be detected and restyled
but
This single bold word would be skipped.

EricFletcher
04-25-2013, 07:04 AM
From your example, I assume you want to find a line or paragraph that has been set with bold rather than a specific style that sets the bold. If so, this will do it:

Find what: (?[!^0013]{1,})(^0013)
Options: Use Wildcards
Format: Font: Bold <-- you can press Ctrl-B to turn this on, or use Format > Font

Replace with: \1\2
Format: Font: Not Bold, Font color: Red <-- use Format > Font to set this
Replace All will set the bold line (paragraph) in your example to red and not bold. The expression finds any number of characters preceding a paragraph mark (the ^0013) set in bold by using 2 phrases: one for any characters except the ¶; the other for the ¶ itself.

Note that this method has limitations: it will incorrectly change a string set in bold that happened to be at the end of a paragraph and it won't work if the paragraph mark is not set in bold with the rest of the line.

If you want to detect if a style using bold in its definition has been applied, you could just use a straightforward F&R to do so--and use the replace to change it to a different style. Use of the bold font attribute (or even a bold character style) would be ignored. For example, if your bolded line was set as a Heading 3, and you wanted it to become Subtitle instead:

Find what: <-- leave empty
Format: Style: Heading 3 <-- use Format > Style, and choose Heading 3

Replace with: <-- leave empty
Style: Subtitle <-- use Format > Style, and choose Subtitle

Hope this helps...

macropod
04-25-2013, 04:47 PM
If you're working with paragraph Styles, then the whole paragraph necessarily carries that Style, regardless of whether any of its content has been formatted differently. You thus set the paragraph Style name as one of the Find parameters and the replacement Style as one of the Replace parameters. There is no need to specify 'bold' or any other attribute, unless you're concerned to exclude paragraphs in that Style that have some content not conforming to the paragraph Style (eg a character Style has been applied). The wildcard Find expression is nothing more than *.

eggre
04-25-2013, 08:16 PM
Thanks, everyone. I see I need to have provided more information. I was trying to be concise, to a fault.

Rephrased:
I have a font style called CLI. When CLI is applied to an entire paragraph (of any paragraph style), I would like my F&R to change the paragraph style to Heading 4. When CLI appears intermittently in a paragraph, I want the F&R to ignore it.

Eric's first method has some promise. I need to figure out a way to defeat false hits.

macropod
04-25-2013, 08:41 PM
You cannot do it with Find/Replace on its own - it needs a macro. Try:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[!^13]{1,}"
.Style = "CLI"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Start = .Paragraphs.First.Range.Start Then
If .End + 1 = .Paragraphs.First.Range.End Then
.Paragraphs.First.Style = "Heading 4"
End If
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
Note: The above code will applies the Heading 4 Style without removing the character Style. It also updates the Style if the only charater not formatted in te CLI Style is the paragraph break. By not including a terminating paragraph break in the Find expression, the macro also works with table content terminated by a cell marker. To erase any non-standard formatting, you could use:
.Paragraphs.First.Range.Font.Reset
(or just .Font.Reset)
but that would wipe out other character formats not in the CLI & Heading 4 Styles as well.

eggre
04-25-2013, 09:34 PM
Booyah! Thanks Paul! That works fabulously.

There were a few contexts I didn't anticipate (like bold text in a table cell), so I'm thinking I'd better flag text rather than going ahead and restyling it. If I may impose one last time, in your macro, how would I highlight the paragraph instead of restyling it Heading 4?

macropod
04-25-2013, 09:41 PM
To highlight, you could replace:
.Paragraphs.First.Style = "Heading 4"
with:
.Paragraphs.First.Range.HighlightColorIndex = wdBrightGreen

If you wanted to exclude table content from the re-styling, you could wrap the existing If ... End If block in:
If .Information(wdWithInTable) = False Then
...
End If

eggre
04-25-2013, 09:49 PM
Works better than I dared dream. Many thanks. I owe you a beer.