Style name problem

R

robot

Hi All,

When my cursor is somewhere inside a certain paragraph, and i run the
following line of code:

MsgBox Selection.Paragraphs(1).Style

However, the result is different from the style name that I see in the style
listbox!

Why is that? How can I retrieve the style name in the listbox? Any help will
be very much appreciated!

I use Win XP and Word XP.
 
S

Stefan Blom

The Style box displays the style applied to the selected text. That style
isn't necessarily a *paragraph* style, though: it could very well be a
*character* style, which can be applied to whole paragraphs or to parts of
paragraphs.

In theory, you could use MsgBox
Selection.Paragraphs(1).Range.Style.NameLocal to return the name of the
character style applied to the specified paragraph, but since a paragraph
may have several different character styles applied to it, you would have to
use something like this instead:

Sub TestMacro()
Dim c As Range
For Each c In Selection.Paragraphs(1).Range.Characters
Debug.Print c.Style.NameLocal, c.Style.Type
Next c
End Sub

The test macro prints, to the Immediate window of the Visual Basic Editor,
the name of the character or paragraph style applied to each character in
the specified paragraph. It also lists the type of style found: 1 means
paragraph style, and 2 means character style. For a complete list of style
types, look at the wdStyleType constants in the Object Browser.

--
Stefan Blom
Microsoft Word MVP


in message news:%[email protected]...
 
K

Klaus Linke

BTW, Word2007 has new Range.ParagraphStyle, Range.CharacterStyle,
Range.TableStyle and Range.ListStyle properties, that get you around the
problems older versions had.
A lot of people (me certainly) had been asking for that a while...

Regards,
Klaus
 
R

robot

Thank you for your reply.

However, I am sure that I use only paragraph styles.

Also, I look up VBA help, and found that
Selection.Paragraphs(1).Style.NameLocal and Selection.Paragraphs(1).Style
are the same, as NameLocal is the default property of 'Style'. Either one
will return the name of the paragraph style applied to the selected
paragraph.

I am still sitting on my chair, staring at the styles listbox, and wondering
why Selection.Paragraphs(1).Style should return something different.
 
S

Stefan Blom

.... but of course it doesn't solve the problem when a range object has
several different styles applied to it.
 
S

Stefan Blom

What is displayed in the Styles box, exactly?

Are you seeing entries such as Normal + Bold? That would indicate a style
name *plus* direct formatting (and it can be turned off in the Word Options
dialog box).

--
Stefan Blom
Microsoft Word MVP


in message news:%[email protected]...
 
K

Klaus Linke

Also, Word 2002 and 2003 will create character styles automatically if you
apply a paragraph style to part of a paragraph. Nothing much can be done to
prevent that (and it happens easily enough accidentally).

And if you have tables, you can't prevent using table styles (...the default
one being used is "Table Grid", I think).

Klaus
 
K

Klaus Linke

Stefan Blom said:
... but of course it doesn't solve the problem when a range object has
several different styles applied to it.

Well, you can get them all individually. Can't see what a better solution
than Range.CharacterStyle, .ParagraphStyle, .ListStyle, .TableStyle could
have looked like to access them?

Klaus
 
S

Stefan Blom

I don't disagree. :)

My point was that it can still be a relatively complex task to figure out
which styles are applied to a range object.
 
K

Klaus Linke

Hi Stefan,

Yes, sorry, I misread you!
The other way is pretty cumbersome and slow through the Word Object Model,
too (assigning paragraph and character styles, bold, italic, ..., creating
tables...).
I'm just now trying to import a lot of tagged text from QuarkXPress, and
experiment with turning the Quark tags into HTML tags, so I don't have to
wait half an hour for the formatting macro to finish.

Klaus
 
S

Stefan Blom

Also, I look up VBA help, and found that
Selection.Paragraphs(1).Style.NameLocal and Selection.Paragraphs(1).Style
are the same, as NameLocal is the default property of 'Style'. Either one
will return the name of the paragraph style applied to the selected
paragraph.

You can certainly make use of default properties in VBA.

The reason I tend to explicitly specify even default properties is that it
makes the code a little easier to read.
 

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