Extracting range from Section

B

Bo Rasmussen

Hi,

I have a document that contain a lot of sections. Each section starts with
an 4 characte identifier, which I would like to extract. Now I would like to
iterate through the sections, but receive a compiler error (wrong number of
arguments)

Dim oSection As Section
Dim oRange as Range

For Each oSection In ActiveDocument.Sections
oRange = oSection.Range(0, 4) 'This is not accepted by compiler
oRange = oSection.Range() 'This is accepted by compiler
Next oSection

'This is also OK
Set oRange = ActiveDocument.Range(0, 4)

What is the difference between the Range object returned by a Section and
that returned by ActiveDocument. And what should I actually iterate through
the sections?

Kind regards
Bo Rasmussen
 
T

Tony Jollans

Yes, it is a bit confusing, isn't it?

Document objects have a Range *method* which can (optionally) take a start
and end position to return a range which is a subset of the document range.

Other objects have a Range *property* which simply returns the complete
range belonging to the object.

I guess the underlying reason is to do with the fact that range starts and
ends are all measured relative to the document.
 
B

Bo Rasmussen

Hi again,

Hmmm- do you know what I should do to obtain my goal then? I simply want to
iterate through the sections and retrieve the first 4 characters in each
section.

Kind regards
Bo Rasmussen
 
G

Greg

Maybe:
Sub Test()
Dim oSection As Section
Dim oRange As Range

For Each oSection In ActiveDocument.Sections
Set oRange = oSection.Range
oRange.Collapse wdCollapseStart
oRange.MoveEnd wdCharacter, 4
MsgBox oRange
Next oSection


End Sub
 

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