Odd Behaviour with a range object

G

Greg

I was trying to apply a style to the first sentence of each paragraph
in a document.

For example:

This is paragraph 1 sentence 1. This is paragraph 1 sentence 2. Etc.
This is paragraph 2 sentence 1. This is paragraph 2 sentence 2. Etc.


Using this code you will see the message box accurately reports the
first sentence text, but the .style is applied to the entire paragraph.

Sub Scratchmacro1()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
MsgBox oPar.Range.Sentences(1)
oPar.Range.Sentences(1).Style = "First Sentence"
Next oPar
End Sub

However, if I first select the defined range then apply the style all
is well:

Sub Scratchmacro2()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
oPar.Range.Sentences(1).Select
Selection.Style = "First Sentence"
Next oPar
End Sub

Can anyone explain this behaviour?

Thanks.
 
H

Helmut Weber

Hi Greg,

seems you are applying a paragraph style,
the scope of which is the paragraph. ;-)

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg

Helmut,

Actually I have defined "First Sentence" as a Character Style.
Regardless, you appear to be correct.
 
G

Greg

This just more proof that I am still stumbling in the dark wrt object,
methods, properties, etc.

Sometimes I think that there is no chance that the "click" will ever
occur.

Thanks for the slight course correction.

Greg
 
J

Jay Freedman

Hi Greg,

I don't think you're going to want to hear this... I repeated your
experiment, and it applied the character style only to the first sentence of
each paragraph. There was no unexplainable behavior.

This is Word 2003 on Windows 2000. If I get time tonight I'll try it in a
couple of other configurations.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Greg Maxey

Jay,

Confirming your observations. It works as expected here at home using
Word2003. Must be something changed in the Word (let me guess) object
module. Correct?

Thanks.
 
J

Jay Freedman

Nope. The macro works perfectly here in Word 2000 on Windows XP
(running in Virtual PC).

Are you sure the style you defined before was a character style, not a
paragraph style? Or maybe there's something odd about its definition.

Jay
 
G

Greg Maxey

Jay,

OK. No I am not sure but I will check tomorrow. Since it is working for
you it must be something simple and boneheaded that I did. Thanks.
 
G

Greg

Jay,

As expected, I must have goofed creating the style. I didn't keep the
project I used yesterday, but recreating today (the correct way) worked
just fine. Thanks for doing the experiment.

Greg
 

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