PDA

View Full Version : Solved: Select and change style



Ken Puls
11-01-2005, 02:18 PM
Hello,

I'm trying to figure out how, if I have my cursor in a table cell, to select all text in the cell that has "Normal" style applied and apply Body Text style.

Can anyone give me a pointer?

Thanks!

Ken Puls
11-01-2005, 05:06 PM
I think that this will work for my purposes... far from the most efficient, I'm sure, but satisfactory. :)

Sub ChangeStyle()
Dim lWord As Long
For lWord = 1 To Selection.Words.Count
If Left(Selection.Words(lWord).Style, 6) = "Normal" Then Selection.Words(lWord).Style = "Body Text"
Next lWord
End Sub

TonyJollans
11-01-2005, 05:30 PM
Hi Ken,

You would probably be better off using Find and Replace. Just record yourself to get some basic code.

Ken Puls
11-01-2005, 05:35 PM
Hi Tony,

Not being really familiar with the Word model... can you do a find/replace on Styles?

Ken Puls
11-01-2005, 05:42 PM
Sorry Tony! Found it!

Go figure the neat stuff they hide in all those little boxes! ;)

Cleaned up version of (necessary) recorded code:
With Selection
.Find.Style = ActiveDocument.Styles("Normal")
.Find.Replacement.Style = ActiveDocument.Styles("Body Text")
.Find.Execute Replace:=wdReplaceAll
End With


Will still need to edit it to be appropriate to cell only, though. :)

TonyJollans
11-01-2005, 05:58 PM
Haven't tried this but it should work with ..

With Selection.Cells(1).Range.Find

The Find object can be a bit funny with Ranges sometimes (or maybe I just haven't got a proper grip on it).

Ken Puls
11-02-2005, 10:25 AM
Hi Tony,

Ah! Now cells... I can work with those! LOL!

Worked a treat, my friend. Thank you very much. :thumb