Determining whether a range is inside a field

A

Andy Bowles

Version: Word 2000 SR-1

I have some text that I need to use within an If field. The text may
contain double quotes, and may also contain other fields.

Because the If field treats a double quote as delimiting an argument, I want
to replace any double quotes with a Symbol field representing the quote.
However, if a double quote is within a field, I want to leave it unchanged.

I can do the first part like this:

Private Sub Replace_Quotes(sel as Word.Selection)
While Find_Text(sel, """")
sel.Fields.Add sel.Range, wdFieldSymbol, "34", False
Wend
End Sub

Private Function Find_Text(sel As Word.Selection, search_text As String)
With sel.Find
.ClearAllFuzzyOptions
.ClearFormatting
.Forward = True
.MatchWholeWord = False
.MatchWildcards = False
.Text = search_text
.Wrap = wdFindStop
Find_Text = .Execute
End With
End Function

This works, but that if my selection contains a field like this:

{ TIME \@ "dd MMMM yyyy" }

the quotes inside the field also get replaced with the Symbol field.

So, is there any way to determine whether sel.Range relates to something
that's inside a field?

If there isn't, I suppose I could do it by recording each field's definition
before I start, and then restoring each field to it's original state. Is
there a better way?

Thanks.

Andy Bowles
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Andy,

Use

Selection.Range.Fields.Count

If the selection contains a field, the count will be greater than 0.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
A

Andy Bowles

Doug Robbins - Word MVP said:
Hi Andy,

Use

Selection.Range.Fields.Count

If the selection contains a field, the count will be greater than 0.

That seems to work only if the entire field is contained within the Range.

However, when I have searched for a double quote and found one within a
field, Selection.Range comprises just the double quote, and
Range.Fields.Count is zero.

I think what I'm looking for is something like "Selection.Range.Information
wdWithInField", which seems not to exist.

I have thought of a solution which is an improvement on my earlier idea of
recording and rewriting each field's contents, though it's still a bit
clumsy: I can start by building an array of the ranges of the existing
fields, and then for each double quote that I find use the InRange method to
test whether it's inside a field.
 
P

Peter Hewett

Hi Andy

Try the following code, if the selection is an insertion point (IP) and
it's in a field this function will return true. It will also return true if
the selection contains one or more fields:

Public Function SelectionContainsAField() As Boolean
Dim rngTemp As Word.Range
Dim fldItem As Word.Field

If Selection.Type = wdSelectionIP Then

' Extend range to include current paragrpagh,
' if Selection is in a field
' the range is expanded to include that field
Set rngTemp = Selection.Range
rngTemp.Expand wdParagraph

' Make sure IP is really in a field and it's not just
' the expanded paragragh that picked up a field
For Each fldItem In rngTemp.Fields
If Selection.InRange(fldItem.Result) Then
SelectionContainsAField = True
Exit For
End If
Next
Else
' If selection is not an IP it must contain
' the whole field if a field is present
SelectionContainsAField = Selection.Fields.Count > 0
End If
End Function

Please note that if the IP is at the very start of a field this the
function will return false as this is not recognised by Word as being IN
the field result.

HTH + Cheers - Peter
 
A

Andy Bowles

Peter Hewett said:
Try the following code, if the selection is an insertion point (IP) and
it's in a field this function will return true. It will also return true if
the selection contains one or more fields:

Public Function SelectionContainsAField() As Boolean
Dim rngTemp As Word.Range
Dim fldItem As Word.Field

If Selection.Type = wdSelectionIP Then

' Extend range to include current paragrpagh,
' if Selection is in a field
' the range is expanded to include that field
Set rngTemp = Selection.Range
rngTemp.Expand wdParagraph

' Make sure IP is really in a field and it's not just
' the expanded paragragh that picked up a field
For Each fldItem In rngTemp.Fields
If Selection.InRange(fldItem.Result) Then
SelectionContainsAField = True
Exit For
End If
Next
Else
' If selection is not an IP it must contain
' the whole field if a field is present
SelectionContainsAField = Selection.Fields.Count > 0
End If
End Function

Please note that if the IP is at the very start of a field this the
function will return false as this is not recognised by Word as being IN
the field result.

HTH + Cheers - Peter

Thank you. That worked (except that in this instance I had to use
fldItem.Code rather than fldItem.Result).

Andy Bowles
 
P

Peter Hewett

Hi Andy

Sorry I did not pick up on the fact that field codes were on.

Cheers - Peter
 

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