Strange results with Style/Font change macro

C

Colleen

I have written the following macro for our firm:

With ActiveDocument.Styles("Normal").Font
.Name = "Times New Roman"
.Size = 12
End With

Selection.WholeStory
Selection.Style = ActiveDocument.Styles("Normal")
Application.GoBack

Our firm default was CG Times until about a year ago. However, often there
are documents that are re-used (don't get me started) and they are still in
the old CGT font. Problem is, sometimes the font was applied via the Normal
style, and sometimes it was directly formatted. At any rate, this macro
tries to cover all the bases. I also wanted to be able to reapply the
Normal style without overriding any direct font attributes (i.e., bold,
underline, etc.). This works perfectly except in this unusual situation.
If the first paragraph with text is formatted with one of those attributes
(i.e., bold), then any bold formatting is removed throughout the document.
Any other attributes, such as italics, are left alone. All direct
formatting is left alone as long as the first paragraph with text in it does
not have any direct font formatting. It's really strange. Even if I put
blank paragraphs above the first one, it still looks at the first paragraph
with text in it as the deciding factor.

Anyone have any ideas on this?

Colleen
 
K

Klaus Linke

Hi Colleen,

It's even a bit weirder:
If more than 50% of the characters in the first paragraph are bold, bold is
removed in all the selection.
Else, bold is kept.

I don't like it either. But Word has quite a few such features where it
tries to guess what the user wants, and uses rules that are hard to fathom.

You could replace CG Times with TNR (using Edit > Replace).

Maybe it would be easier to simply remove the CG Times font file from your
machine(s), and set TNR as the default replacement for it in "Tools >
Options > Compatibility > Font substitution". It's enough to do that once.
And all old documents will then use TNR without your needing to change a
thing.
The documents will still use CG Times if you send them to someone who has CG
Times installed though.

Regards,
Klaus
 
C

Colleen

Very good insights, Klaus. I especially like the option of removing the
font, but we do email our docs to clients a lot and I know the attorneys
wouldn't want any font surprises.

I think the best option is the Edit > Replace option you mentioned. Don't
know why I didn't think of that before, but it works perfectly. I'll just
edit the macro to modify the style, then do the Replace command for any text
that might have been directly formatted.

Thanks so much for your help!

Colleen
 
C

Chuck

Hi Colleen

If you apply Normal style to the entire document, then every paragraph will
be in Normal style, which may not be what you want.

If you want to be more selective you can cycle through all the paragraphs in
the range, test to see what style the paragraph is, then reapply the style to
that paragraph to change the font name without affecting attributes like
bold, italic, etc.

Note that you need to cycle through all the "story ranges" as well to make
sure you hit all headers, footers, footnotes, endnotes, etc.

Here's some sample code to get you started thinking about ways you can use
active document ranges and story ranges... The sample uses "Arial" and
"Batang" font names but obviously will work with any font names, sizes etc
you choose.

sub ChangeStyles()

Dim aPara As Paragraph
Dim aStyle As Style
Dim aStoryRange As Range
Dim sParaStyleName As String

On Error Resume Next

With ActiveDocument

With .Styles("Normal").Font
.Name = "Arial"
.Size = 12
End With

For Each aStoryRange In .StoryRanges

For Each aPara In .Paragraphs

For Each aStyle In .Styles
aPara.Style.Name = aPara.Style.Name
'this reapplies all styles so font
'changes if style is based on Normal
Next aStyle

Next aPara

With aStoryRange.Find
With .Replacement
With .Font
.Name = "Batang"
End With
.Font.Name = "Arial"
End With
.Text = "*"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

Next aStoryRange

End With

end sub
 
O

OughtFour

Klaus said:
Hi Colleen,

It's even a bit weirder:
If more than 50% of the characters in the first paragraph are bold, bold is
removed in all the selection.
Else, bold is kept.

In that case, why not

-insert new paragraph at the start of the document (Normal style), with VBA
-make sure it is not bold, with VBA
-run your code
-delete the paragraph, with VBA

Sometimes a workaround is good enough.
 

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