VBA question

E

Edward

Hi everybody,
What is the difference between these two codes
dim sec as section
if sec.Range.Paragraphs(1).Range.Style="..." then

and
dim sec as section
if sec.Range.Paragraphs(1).Style="..." then

the first version which is a amll pert of my code sometimes generates error
when paragraph marker if off but when I turn on the paragraph marker it works!

so I wonder what does that addition .Range do in the code.
 
T

Tony Strazzeri

Hi everybody,
What is the difference between these two codes
dim sec as section
if sec.Range.Paragraphs(1).Range.Style="..." then

and
dim sec as section
if sec.Range.Paragraphs(1).Style="..." then

the first version which is a amll pert of my code sometimes generates error
when paragraph marker if off but when I turn on the paragraph marker it works!

so I wonder what does that addition .Range do in the code.

Hi Edward,

I'm Using Word 2002 SP3.

Dim sec As Section
Set sec = ActiveDocument.Sections(1)
If sec.Range.Paragraphs(1).Range.Style = "Heading 1" Then MsgBox
"1).Range.Style ="

If sec.Range.Paragraphs(1).Style = "Heading 1" Then MsgBox "1).Style =
"


Both work OK for me under both circumstances.

Hope this helps.

Cheers!
TonyS.
 
E

Edward

Hi Tony,
There must be a difference between these two code I know it works most of
the times but not always ! In fact sometimes there is no
sec.Range.Paragraphs(1).Range.Style ( there is no range for paragraph(1) !)
but for the same situation the following code sec.Range.Paragraphs(1).Style
works another words there is no range for paragraph 1, but there is a style
for paragraph 1. so I dont know the difference between these two codes .
 
T

Tony Strazzeri

Edward said:
Hi Tony,
There must be a difference between these two code I know it works most of
the times but not always ! In fact sometimes there is no
sec.Range.Paragraphs(1).Range.Style ( there is no range for paragraph(1) !)
but for the same situation the following code sec.Range.Paragraphs(1).Style
works another words there is no range for paragraph 1, but there is a style
for paragraph 1. so I dont know the difference between these two codes .

sec.Range.Paragraphs(1).Range
This gets the range of the first paragraph in the document. I believe
there will always be one of these because even a blank document
contains at least one (empty) paragraph.

sec.Range.Paragraphs(1).Range.Style
will return the stylename of the style applied to the paragraph (1)'s
range.

I don't believe there is any functional diffeence betwen that and
sec.Range.Paragraphs(1).Style


Are you making sure that sec is assigned a value in both cases?

in my example I specifically used
Set sec = ActiveDocument.Sections(1)


Hope this helps.
Cheers!

TonyS.
 
F

fumei via OfficeKB.com

Strange, as:

MsgBox ActiveDocument.Range.Paragraphs(1).Style _
& vbCrLf & _
ActiveDocument.Range.Paragraphs(1).Range.Style _
& vbCrLf & _
ActiveDocument.Paragraphs(1).Style

gives three exact same results.for paragraph 1.
 
T

Tony Strazzeri

Strange, as:

MsgBox ActiveDocument.Range.Paragraphs(1).Style _
      & vbCrLf & _
     ActiveDocument.Range.Paragraphs(1).Range.Style _
     & vbCrLf & _
     ActiveDocument.Paragraphs(1).Style

gives three exact same results.for paragraph 1.

That is correct as in all three cases we are still retireving the
style name applied to the same paragraph. The only differece is we are
referring to it via a different way.
 

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