How to make Character Style use underlying Paragraph Font property

C

ColdPost

I have a character style that I want to be displayed with the font size of
the underlying paragraph. I tried:

ActiveDocument.Styles("docemphasis1").Font.Size =
ActiveDocument.Styles("Default Paragraph Font").Font.Size

but doesn't work.
 
S

StevenM

To: ColdPost,

I would hope that there is a better way of doing this, but the following is
what I came up with.

You can rename "Char" anything you want.

Sub MakeCharStyleSizeSameAsBasedOn()
Dim sStyle As String
Dim s As String
Dim i As Long

sStyle = "Char"
s = ActiveDocument.Styles(sStyle).Description
i = InStr(1, s, "Based on:")
s = Right(s, Len(s) - (i + Len("Based on:")))
i = InStr(1, s, ",")
If i > 0 Then s = Left(s, i - 1)
ActiveDocument.Styles(sStyle).Font.Size =
ActiveDocument.Styles(s).Font.Size
End Sub

Steven Craig Miller
 
C

ColdPost

Hi, I tried it but didn't work either. The character styles I want to modify
are all based on "Default Paragraph Font" so the string processing does not
change my original instruction. :(
 
L

Lene Fredborg

I am not quite sure whether you want to use the font size that is actually
applied to the text or the font size of the "clean" paragraph style (direct
formatting may have been applied). The following two examples may help you:

The following code line will find the font size applied to the first
character in the selection and change the style definition of the character
style “MyCharStyle†to that size:

ActiveDocument.Styles("MyCharStyle").Font.Size =
Selection.Characters(1).Font.Size

The following code line will find the font size of the paragraph style that
is applied to the first paragraph in the selection and change the style
definition of the character style “MyCharStyle†to that size:

With ActiveDocument
.Styles("MyCharStyle").Font.Size =
..Styles(Selection.Paragraphs(1).Style).Font.Size
End With

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
G

Greg

I'm no expert, so FWIW.

My understanding is the "Default Paragraph Font" is not properly a style at
all. Rather, its meaning depends on context.

In quick & unscientific tests:

In the new style dialog, the font size applied when a new character style is
based on "Default Paragraph Font" is the size of the selection.

ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Size

returns the size of the NormalStyle font, regardless of selection.

and applying "Default Paragraph Font" to a character formatted with a small
font in a paragraph formatted with a large font sets the character to the
NormalStyle size.

But if the paragraph font is embedded in a paragraph style, the character is
set to the size of the paragraph style.

Of course, the size of the selected paragraph is easy to find:

ActiveDocument.Paragraphs(1).Range.Font.Size

I find that I can set a character style to this size without incident.

As I said, I'm no expert.

Just a few quick tests.

Greg
 
S

StevenM

To: Greg,

Greg said:
Of course, the size of the selected paragraph is easy to find:

ActiveDocument.Paragraphs(1).Range.Font.Size

I find that I can set a character style to this size without incident.

That does not appear to be true if the paragraph contains characters of
different size.

Steven Craig Miller
 
S

StevenM

To: ColdPost,

Take 2:

' Macro: MakeCharStyleFontSizeSameAsUnderlyin (Paragraph Font Size)
' Looks for every character with sStyle in the Active Document
' It then looks for the last character in that paragraph
' Once it finds a character not of the same style as sStyle
' It changes the size of the sStyle character font
'
Sub MakeCharStyleFontSizeSameAsUnderlying()
Dim oChar As Range
Dim oRange As Range
Dim sStyle As String
Dim s As String
Dim i As Long
Dim start As Long

sStyle = "Char"
For Each oChar In ActiveDocument.Characters
If oChar.Style = sStyle Then
Set oRange = oChar.Paragraphs(1).Range
start = oRange.start
oRange.Collapse CollapseEnd
oRange.End = oRange.End - 1
While start < oRange.start And oRange.Style = sStyle
oRange.End = oRange.End - 1
Wend
oChar.Font.Size = oRange.Font.Size
End If
Next oChar
End Sub

Steven Craig Miller
 
C

ColdPost

Neither of both. I don't want to change to a static font size whether it's
the paragraph's current one or its style's one. I want to set it to the
underlying pararaph font whatever it is now or even if it is changed later,
dynamically. This is the behavior of styles based on the "Default Paragraph
Font" but what I need is to do this for a property (font size in the
example) that has already been set.
 
C

ColdPost

Greg said:
I'm no expert, so FWIW.

My understanding is the "Default Paragraph Font" is not properly a style
at
all. Rather, its meaning depends on context.

In quick & unscientific tests:

In the new style dialog, the font size applied when a new character style
is
based on "Default Paragraph Font" is the size of the selection.

ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Size

returns the size of the NormalStyle font, regardless of selection.

and applying "Default Paragraph Font" to a character formatted with a
small
font in a paragraph formatted with a large font sets the character to the
NormalStyle size.

But if the paragraph font is embedded in a paragraph style, the character
is
set to the size of the paragraph style.

Of course, the size of the selected paragraph is easy to find:

ActiveDocument.Paragraphs(1).Range.Font.Size

I find that I can set a character style to this size without incident.

As I said, I'm no expert.

Just a few quick tests.

Greg
 
C

ColdPost

Your solution is a static change; if the paragraph's font size changes you
need to rerun the macro again. What I'm looking for is this:
A character style originally created based on "Default Paragraph Font" and
an empty (as shown in the modify style dialog) attribute field (for example,
Font Size) is always set to the font size of the underlying paragraph. i.e.:
If you change the paragraph's font size, the range of the character style
adjusts accordingly. I want to be able to do this for a character style of
my choosing.
 
C

ColdPost

Yes, I get the same results as you. But a character style originally created
based on "Default Paragraph Font" and a Font Size attribute empty was always
set to the font size of the underlying paragraph. i.e.: You changed the
paragraph's font size and the range of the character style adjusts
accordingly. So, I want to be able to do this for a character style of my
choosing.
 
K

Klaus Linke

It's a nuisance, isn't it? Sometimes it's easier to define the style from
scratch in a dummy document, and copy it into the existing document using
the organizer.

Or you could search for the character style in the document, then change the
style definition through the dialog:

Dim dlg As Dialog
Set dlg = Dialogs(wdDialogFormatDefineStyleFont)
With dlg
.Points = ""
.Execute
End With

Obviously, that only works if you do have some text in the doc that's
formatted in the character style you want to change...

Seems to me you should be able to reset the character style's font size
using
ActiveDocument.Styles("myCharStyle").Font.Size = wdUndefined

But that doesn't work, unfortunately.

Regards,
Klaus
 
C

ColdPost

In the code you provided how does VBA know to which style the font
properties apply?
Anyhow, I tried this one and it worked!:

Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveDocument.Styles("docemphasis1").Font
.Name = ""
.Italic = True
End With
ActiveDocument.Styles("docemphasis1").BaseStyle = "Default Paragraph
Font"
End Sub

Just didn't have to mention the font size! Isn't it wierd?
 

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