Determining whether a range is in a field

M

Mcollette

How can I write an If statement to determine whether a certain range is
within a field?
 
T

Tony Jollans

This is not straightforward. Do you want to know if you are within a
particular field or, more generally, just whether or not the range
intersects any fields?

If the former, then compare the range's start and/or end against the field's
start and end. If the latter, then you need to check something like this:

With ActiveDocument.StoryRanges(Selection.StoryType)
Discrepancy = -.Fields.Count
.SetRange ActiveDocument.StoryRanges(Selection.StoryType).Start,
Selection.Start
Discrepancy = Discrepancy + .Fields.Count
.SetRange Selection.End,
ActiveDocument.StoryRanges(Selection.StoryType).End
Discrepancy = Discrepancy + .Fields.Count
End With

If Discrepancy <> 0 then
' The Range intersects at least one field
endif
 
M

Mcollette

Sorry, I should have been more specific. I want to determine whethe
the range is within any TOC field in the document
 
T

Tony Jollans

Unfortunately that is perhaps even more complex. Without knowing more of
your documents and what you are trying to do, I can't be sure of the best
way, but the first thing I would try would be to determine and remember
where the Range of each TOC field is, and then compare the start and end of
your Range with those of the saved TOC field Ranges.
 

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