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.

Re: Odd Behaviour with a range object by Helmut

Helmut
Wed Mar 29 13:22:27 CST 2006

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"


Re: Odd Behaviour with a range object by Greg

Greg
Wed Mar 29 13:49:37 CST 2006

Helmut,

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


Re: Odd Behaviour with a range object by Greg

Greg
Wed Mar 29 13:53:00 CST 2006

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


Re: Odd Behaviour with a range object by Jay

Jay
Wed Mar 29 14:13:54 CST 2006

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Greg wrote:
> 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.



Re: Odd Behaviour with a range object by Greg

Greg
Wed Mar 29 14:36:24 CST 2006

Jay,

Thanks for the reply. I am using Word2000 here.

Greg


Re: Odd Behaviour with a range object by Greg

Greg
Wed Mar 29 16:45:33 CST 2006

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.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

"Jay Freedman" <jay.freedman@verizon.net> wrote in message
news:%23ZMxO12UGHA.5496@TK2MSFTNGP11.phx.gbl...
> 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 FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup
> so
> all may benefit.
>
> Greg wrote:
>> 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.
>
>



Re: Odd Behaviour with a range object by Jay

Jay
Wed Mar 29 19:43:21 CST 2006

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

On Wed, 29 Mar 2006 17:45:33 -0500, "Greg Maxey"
<gmaxey@mvps.oSCARrOMEOgOLF> wrote:

>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.
>
>--
>Greg Maxey/Word MVP
>See:
>http://gregmaxey.mvps.org/word_tips.htm
>For some helpful tips using Word.
>
>"Jay Freedman" <jay.freedman@verizon.net> wrote in message
>news:%23ZMxO12UGHA.5496@TK2MSFTNGP11.phx.gbl...
>> 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 FAQ: http://word.mvps.org
>> Email cannot be acknowledged; please post all follow-ups to the newsgroup
>> so
>> all may benefit.
>>
>> Greg wrote:
>>> 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.
>>
>>
>

Re: Odd Behaviour with a range object by Greg

Greg
Wed Mar 29 19:49:35 CST 2006

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.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Jay Freedman wrote:
> 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
>
> On Wed, 29 Mar 2006 17:45:33 -0500, "Greg Maxey"
> <gmaxey@mvps.oSCARrOMEOgOLF> wrote:
>
>> 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.
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>> http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
>>
>> "Jay Freedman" <jay.freedman@verizon.net> wrote in message
>> news:%23ZMxO12UGHA.5496@TK2MSFTNGP11.phx.gbl...
>>> 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 FAQ: http://word.mvps.org
>>> Email cannot be acknowledged; please post all follow-ups to the
>>> newsgroup so
>>> all may benefit.
>>>
>>> Greg wrote:
>>>> 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.



Re: Odd Behaviour with a range object by Greg

Greg
Thu Mar 30 13:22:06 CST 2006

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