looping to change a font size

L

Lighthouse

I'm trying to write a silly little macro that goes to every table in an
active document, looks for a particular style, then sets that selection
to 9 points. I have this:

For Each aTable In ActiveDocument.Tables
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Note")
Selection.Style.Font.Size = 9


Obviously it doesn't work (or I wouldn't be here). When I run this
macro it seems to change whatever style the cursor happens to be at at
the time it starts, from that point forward. it never even seems to get
"into" the tables. If i have the cursor inside a table when i run the
macro it seems to do what i want. I need this to be an auto-open macro.


Any advice? Thanks.
 
J

Jay Freedman

Lighthouse said:
I'm trying to write a silly little macro that goes to every table in
an active document, looks for a particular style, then sets that
selection to 9 points. I have this:

For Each aTable In ActiveDocument.Tables
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Note")
Selection.Style.Font.Size = 9


Obviously it doesn't work (or I wouldn't be here). When I run this
macro it seems to change whatever style the cursor happens to be at at
the time it starts, from that point forward. it never even seems to
get "into" the tables. If i have the cursor inside a table when i run
the macro it seems to do what i want. I need this to be an auto-open
macro.


Any advice? Thanks.

If the style named Note occurs only in tables, or if you want it to be 9 pt
wherever it occurs, then what you want to do is NOT a find but just to
modify the style:

Sub AutoOpen()
On Error Resume Next ' in case Note doesn't exist
ActiveDocument.Styles("Note").Font.Size = 9
End Sub

If the style is used both inside and outside tables, and you want the text
in that style to be overridden with direct formatting of 9 pt size, then you
need something more complicated -- but I don't want to get into that if you
don't need it.
 
T

Tony Jollans

You must Execute the Find - all that shows in your code snippet is
(partially) setting it up

For Each aTable In ActiveDocument.Tables
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Note")

Selection.Find.Execute

Selection.Style.Font.Size = 9

There is, of course, more to it. You should check whether the Find has
actually found anything. One way is to check the return from the Find ...

For Each aTable In ActiveDocument.Tables
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Note")

If Selection.Find.Execute Then

Selection.Style.Font.Size = 9
' etc.

The next thing you need to be aware of is that although you have a For Each
loop, your Find is working on the Selection. It would be better done on the
individual table ..

aTable.Range.Find.ClearFormatting
' etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top