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 2006

Tip: Looking for answers? Try searching our database.

Odd Behaviour with a range object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 29 Mar 2006 19:47 GMT
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.
Helmut Weber - 29 Mar 2006 20:22 GMT
Hi Greg,

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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg - 29 Mar 2006 20:49 GMT
Helmut,

Actually I have defined "First Sentence" as a Character Style.
Regardless, you appear to be correct.
Greg - 29 Mar 2006 20:53 GMT
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
Jay Freedman - 29 Mar 2006 21:13 GMT
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.

Signature

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.

> I was trying to apply a style to the first sentence of each paragraph
> in a document.
[quoted text clipped - 32 lines]
>
> Thanks.
Greg - 29 Mar 2006 21:36 GMT
Jay,

Thanks for the reply.  I am using Word2000 here.  

Greg
Greg Maxey - 29 Mar 2006 23:45 GMT
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.

Signature

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

> Hi Greg,
>
[quoted text clipped - 42 lines]
>>
>> Thanks.
Jay Freedman - 30 Mar 2006 02:43 GMT
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

>Jay,
>
[quoted text clipped - 50 lines]
>>>
>>> Thanks.
Greg Maxey - 30 Mar 2006 02:49 GMT
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.

Signature

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

> Nope. The macro works perfectly here in Word 2000 on Windows XP
> (running in Virtual PC).
[quoted text clipped - 73 lines]
>>>>
>>>> Thanks.
Greg - 30 Mar 2006 20:22 GMT
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
 
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.