MSWord...styles.

L

Luke

How can I get the style name for a given range in my word
document? I've tried several things, and the funny thing
is
Debug.print myStyle
will give me the style name (which is all I want,) but
there doesn't seem to be any way to get this information
from the range or style object and assign it to a string.

I'm a little new at VBA, so any help would be appreciated.

Thanks,
L-
 
S

Stu

In VBA it should be available from the Style property of the Selection
object. So, for example, if you positioned the insertion point in the
middle of the third paragraph, then executed the following VBA code, then
the style name would be stored in mStyle.

Dim mStyle
mStyle = selection.style
msgbox "The style of the selected paragraph is " & mStyle

Stu
 
H

Helmut Weber

Hi Luke,
there must something else be wrong:
Dim S As String
Dim R As Range
Set R = ActiveDocument.Paragraphs(4).Range
S = R.Style
MsgBox S
You get error 91, if the range includes more than one
style. Which is inconsistent compared to returning
other format-properties like format.character, where you
don't get an error, but a constant representing
"various" or "undefined", it is -1 I think, AFAIK.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, W98
 
L

Luke

Hey thanks, I figured out the problem, I miss read the
documentation. I thought the range property: style
returned a style object, but it returns a variant. Which,
is a string in the case, and is also what I was looking
for.

so eh... my mistake.

Thanks anyway.
L-
 
T

Thomas Winter

Luke said:
Hey thanks, I figured out the problem, I miss read the
documentation. I thought the range property: style
returned a style object, but it returns a variant. Which,
is a string in the case, and is also what I was looking
for.

so eh... my mistake.

Thanks anyway.
L-

Luke, that's not quite correct. The Range object's Style property is a
Variant so that you can SET it with either a Style object or a string that's
the name of the style or one of the builtin style constants. When you GET
the Style property, you always get a Style object. The reason you can do S =
R.Style when S is a string is because the default property of the Style
object is NameLocal, which is a string.

-Tom
 

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