PDA

View Full Version : Styles within Word



GazBags
06-16-2012, 11:46 AM
I'm just getting going with VBA (as a late starter) as I've found that there are things I'd like to do within Word etc that it just won't seem to let me do easily without...
Anyway, what I'm trying to do is to change the format of certain things within documents. I try and put chord changes etc into song lyrics as prompts for me when playing along with songs, for instance:

[Em] Nightsin white [D] satin,
[Em] Neverreaching the [D] end
[C] Letters I’ve[G] written,
[F] Nevermeaning to [Em] send.

I use courier (10) for a reason, but that's irrelevant here, what I try to do is have the chords in 12pt, bold and red. I can achieve that with format painter but it means manually changing each instance of the various chords, or I could manually set it each time I type them in, but is there a way to automate it when a document is complete to go through the text and change anything between square brackets (and include the square brackets) to 12pt, bold, red?

gmaxey
06-16-2012, 11:55 AM
Try:

Sub FindAndChangeFontAttributes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "[\[]<*>[\]]"
With .Replacement.Font
.ColorIndex = wdRed
.Size = 12
.Bold = True
End With
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

GazBags
06-16-2012, 12:10 PM
Wow!

Superb, thank you

You make it look quick and easy - guess I've got a lot of learning to do

Thanks again

gmaxey
06-16-2012, 02:46 PM
Actually you don't even need a vba procedure. You could do the same thing with the Find and Replace dialog. This may help:

http://www.gmayor.com/replace_using_wildcards.htm

macropod
06-17-2012, 12:28 AM
Of course, the better way to do this would be to define a 'Chord' Character Style with the bold red 12pt font and, if need be, use that Style as the replacement parameter.

The Find expression could probably also be reduced to \[*\]