Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Style name problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
robot - 07 Mar 2008 07:59 GMT
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.
Stefan Blom - 07 Mar 2008 10:27 GMT
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.

Signature

Stefan Blom
Microsoft Word MVP

> Hi All,
>
[quoted text clipped - 10 lines]
>
> I use Win XP and Word XP.
Klaus Linke - 07 Mar 2008 15:49 GMT
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
Stefan Blom - 10 Mar 2008 10:31 GMT
That is certainly an improvement.

Signature

Stefan Blom
Microsoft Word MVP

> BTW, Word2007 has new Range.ParagraphStyle, Range.CharacterStyle,
> Range.TableStyle and Range.ListStyle properties, that get you around the
[quoted text clipped - 3 lines]
> Regards,
> Klaus
Stefan Blom - 11 Mar 2008 13:38 GMT
... but of course it doesn't solve the problem when a range object has
several different styles applied to it.

Signature

Stefan Blom
Microsoft Word MVP

> That is certainly an improvement.
>
[quoted text clipped - 5 lines]
>> Regards,
>> Klaus
Klaus Linke - 11 Mar 2008 14:34 GMT
> ... 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
Stefan Blom - 11 Mar 2008 15:23 GMT
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.

--
Stefan Blom
Microsoft Word MVP

>> ... but of course it doesn't solve the problem when a range object has
>> several different styles applied to it.
[quoted text clipped - 4 lines]
>
> Klaus
Klaus Linke - 11 Mar 2008 16:30 GMT
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

>I don't disagree. :-)
>
[quoted text clipped - 13 lines]
>>
>> Klaus
robot - 11 Mar 2008 05:47 GMT
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.
Stefan Blom - 11 Mar 2008 14:17 GMT
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).

Signature

Stefan Blom
Microsoft Word MVP

> Thank you for your reply.
>
[quoted text clipped - 9 lines]
> wondering why  Selection.Paragraphs(1).Style should return something
> different.
Klaus Linke - 11 Mar 2008 14:31 GMT
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

> What is displayed in the Styles box, exactly?
>
[quoted text clipped - 16 lines]
>> wondering why  Selection.Paragraphs(1).Style should return something
>> different.
Stefan Blom - 12 Mar 2008 11:46 GMT
> 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.

Signature

Stefan Blom
Microsoft Word MVP

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.