Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clearing Font Name in a Character Style by VBA

13 views
Skip to first unread message

Ron

unread,
Dec 17, 2001, 12:29:39 PM12/17/01
to
I want to clear the font name of a character style by macro, so that
the default paragraph font of the underlying style shows through. (If
I clear the name manually, it does exactly what I want to achieve). I
have tried:

...with
.font.name=""
.font.name=" "
.font.name="""", even
.font.name=Null
But none of them clear the font name.

Any ideas, please?

Klaus Linke

unread,
Dec 20, 2001, 2:01:58 AM12/20/01
to


Hi Ron,

That *is* a terrible nuisance. The way I do it is to define the style
with just the formatting I want in some other doc or template, and
copy it to the document with the styles organizer.

Another way would be to save as RTF and edit the style definitions (in
the header of the RTF file) manually, but that requires some reading
in the RTF specs, and practice.

I complained to Microsoft support, and asked msw...@microsoft.com for
a better interface to change styles. For example in other programs, if
you delete a style you are asked with what style to format the text
that currently uses this style. That would be a nice way to solve such
problems.

Cheers, Klaus


Klaus Linke

unread,
Dec 20, 2001, 2:44:28 AM12/20/01
to
As you said, you can get rid of the font by manually deleting it in
the font dialog.
You can do the same in a macro:

Dim myDlg1 As Dialog

Selection.Collapse (wdCollapseStart)
Selection.Style = ActiveDocument.Styles("myCharacterStyle")
Set myDlg1 = Application.Dialogs(wdDialogFormatDefineStyleFont)
myDlg1.Font = ""
myDlg1.Execute


But this does not work for a lot of other formatting (for example once
you have defined the style as "subscripted", you can only toggle
between "subscripted" and "not superscripted/subscripted").

Regards, Klaus


Ron

unread,
Dec 20, 2001, 9:59:15 AM12/20/01
to
Works perfectly Klaus, thanks.

Regards Ron

Henk van Boeijen

unread,
Dec 24, 2001, 10:37:32 AM12/24/01
to

Klaus Linke <fotosatz...@t-online.de> schreef in berichtnieuws
9vs0vh$t77$02$1...@news.t-online.com...

> Ron <ron.1....@britishairways.com> wrote:
> > I want to clear the font name of a character style by macro, so that
> > the default paragraph font of the underlying style shows through.
> (If
> > I clear the name manually, it does exactly what I want to achieve).
> I
> > have tried:
> >
> > ...with
> > .font.name=""
> > .font.name=" "
> > .font.name="""", even
> > .font.name=Null
> > But none of them clear the font name.
> >
> > Any ideas, please?
>
>
> Hi Ron,
>
> That *is* a terrible nuisance. The way I do it is to define the style
> with just the formatting I want in some other doc or template, and
> copy it to the document with the styles organizer.
>

I do not think it is that much of a nuisance.
Just try the following method:

.font.reset

This clears all character formatting that is applied directly to a text,
thus
leaving only the formatting that is defined by a style.

Henk


Klaus Linke

unread,
Dec 26, 2001, 7:56:38 PM12/26/01
to
Henk van Boeijen <pos...@use-it.demon.nl> schreef:>

> > That *is* a terrible nuisance. The way I do it is to define the
> > style with just the formatting I want in some other doc or
> > template, and copy it to the document with the styles organizer.

>
> I do not think it is that much of a nuisance.
> Just try the following method:
>
> .font.reset
>
> This clears all character formatting that is applied directly to a
> text, thus leaving only the formatting that is defined by a style.
>
> Henk


Hi Henk,

That's a nice answer, but to a different question ;-)

If you try that with styles:
ActiveDocument.Styles("MyStyle").Font.Reset
you'll get an error 5963 "The character or paragraph format of a style
can not be reset".

Ron had a character style with a font in the style definition, and
he wanted to re-define the style to have *no* font defined (so
that the font of the underlying paragraph style shows through).

I take the chance for another macro that uses the method I
described in my first reply:
Define the style in another template/doc and copy it with the
organizer.

That way you don't run into any of the limitations you usually have.

Greetings, Klaus


Sub DefineStyles()

' You need to "Apply" the style after you have defined it,
' not "Close" the dialog.

Dim myDlg As Dialog
Dim myStyleName As String
Dim DotPath As String
Dim myDoc As Document
Dim myDot As Document
Set myDoc = ActiveDocument


ChangeFileOpenDirectory _
Options.DefaultFilePath(Path:=wdUserTemplatesPath)
' I keep a "virgin" Normal.Dot without style definitions (Base.Dot):
' Documents.Open FileName:="Base.dot"
' else use Normal.Dot:
Documents.Open FileName:="Normal.dot"
DotPath = ActiveDocument.FullName
Set myDot = ActiveDocument
myDot.Content.Select

Set myDlg = Application.Dialogs(wdDialogFormatStyle)
myDlg.Show
myStyleName = myDlg.Name

myDot.Save
myDoc.Activate
Application.OrganizerCopy source:=DotPath, _
Destination:=myDoc.Name, Name:=myStyleName, _
Object:=wdOrganizerObjectStyles

' Undo changes to template:
While myDot.Undo
Wend
myDot.Close SaveChanges:=wdSaveChanges

Set myDlg = Nothing

End Sub

H. van Boeijen

unread,
Jan 2, 2002, 5:57:28 AM1/2/02
to
Hi Klaus,

Aparently I misinterpreted the problem. Still, I think the solution
can be a bit more simple than your suggestion. The fact is that Word
deletes a formatting option in a style definition when it is equal to
the same formatting option in the superseding style definition.

E.g. The style "Special" is based on the style "Normal". The font name
in Normal is "Times new roman" and the font name in Special is
"Arial". If you change the font name in Special to "Times new roman"
you effectively delete the font setting in the style "Special".

Thus, the following code clears a font setting in a given style:


Private Sub ClearStyleFont(ByVal StyleName As String)

Dim oStyle As Style

Set oStyle = ActiveDocument.Styles(StyleName)
oStyle.Font.Name = oStyle.BaseStyle.Font.Name
Set oStyle = Nothing

End Sub

"Klaus Linke" <fotosatz...@t-online.de> wrote in message news:<a0dq51$sf8$07$1...@news.t-online.com>...

Klaus Linke

unread,
Jan 2, 2002, 8:41:21 PM1/2/02
to
Hi Henk,

That definitely is another way to get rid of the font setting.

I still think it's hard to re-define character styles: For example
your macro works nicely in the scenario you described, but fails if
the character style is based on another character style.
Simple solutions also can't get rid of other settings like subscript,
underlined ...

So I find it less complicated in the long run to define character
styles from scatch, and to then copy them into the document.

Greetings, Klaus

Ron

unread,
Jan 8, 2002, 7:02:16 AM1/8/02
to
Klaus/Henk
Thanks for giving me further choices. Both look good for the actual situation.
Regards Ron

Dave Rado

unread,
Jan 8, 2002, 8:26:11 AM1/8/02
to
Hi Klaus

I must be missing something; I just created a character style "temp1", based
on the "Normal" paragraph style, which had the font name defined as Times
New Roman.

I defined "temp1" to have the Arial font, so it became: "Normal + Font:
Arial".

Then I created a second character style "temp1", based on "temp", and with
the font defined as Symbol, so it became "temp + Font: Symbol".

Then I ran Henk's code:

Dim oStyle As Style

Set oStyle = ActiveDocument.Styles(StyleName)
oStyle.Font.Name = oStyle.BaseStyle.Font.Name
Set oStyle = Nothing

And the "temp1" style changed to Arial in the document, and it's style
definition changed to "Temp +"

Is there a way I'm missing to make Henk's code fail?

Regards

Dave


"Klaus Linke" <fotosatz...@t-online.de> wrote in message

news:a10bdh$3lv$04$1...@news.t-online.com...

Klaus Linke

unread,
Jan 8, 2002, 7:40:01 PM1/8/02
to
Hi Dave,

Perhaps I was nitpicking, but the original question was to remove the
font from the character style altogether, so that the font of the
underlying paragraph style is used. As you say, after running Henk's
code, the font used for "temp1" is Arial, not TNR.

Klaus


"Dave Rado" <dr...@onetel.net.uk> schrieb im Newsbeitrag
news:OziOOhEmBHA.2432@tkmsftngp02...

Dave Rado

unread,
Jan 8, 2002, 7:36:19 PM1/8/02
to
Hi Klaus

I see what you're driving at now. The font was removed from the character
style by yje code, but in this instance you also wanted to remove the
linkage with the character style it had been based on.

Regards

Dave


"Klaus Linke" <fotosatz...@t-online.de> wrote in message

news:a1g2ru$bel$07$1...@news.t-online.com...

Klaus Linke

unread,
Jan 8, 2002, 11:19:05 PM1/8/02
to
Hi Dave,

Exactamente (and yes, I really was nitpicking).

But we are off on a tangent now. To get back to my original complaint
and the reason for my "DefineStyles" macro:
Define a paragraph style "UnderlinedPara" with a single underline and
a character style "Underlined" with a double underline. Now apply the
character style to some standard text (without underline), and to some
text in "UnderlinedPara".

Now you can not change "Underlined" with "Format > Styles" in a way
that the underline of the paragraph style is used in both cases; but
you could with my macro.

I admit that is a rather rare case. I run into this problem relatively
often because I like to use colored character styles and paragraph
styles in cleaning up documents. I do that so I can easily see if
styles have been correctly applied. With the user interface, I can't
remove the color definition from a character style.

Also, sometimes it's nice to be able to redefine a style from scratch
with "DefineStyles".

Klaus

0 new messages