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

Re: Determining whether a range is inside a field by Doug

Doug
Sun Jan 25 05:21:58 CST 2004

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
"Andy Bowles" <andy.bowles@virgin.net> wrote in message
news:OM$SKJz4DHA.2888@tk2msftngp13.phx.gbl...
> 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
>
>



Re: Determining whether a range is inside a field by Andy

Andy
Sun Jan 25 13:03:17 CST 2004

"Doug Robbins - Word MVP" wrote:
> 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.



Re: Determining whether a range is inside a field by Peter

Peter
Sun Jan 25 21:15:39 CST 2004

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



"Andy Bowles" <andy.bowles@virgin.net> wrote in
news:ePbLuX34DHA.3224@tk2msftngp13.phx.gbl:

> "Doug Robbins - Word MVP" wrote:
>> 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.
>
>


Re: Determining whether a range is inside a field by Andy

Andy
Tue Jan 27 13:49:28 CST 2004

"Peter Hewett" <Nospam@xtra.co.nz> wrote:

> 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



Re: Determining whether a range is inside a field by Peter

Peter
Tue Jan 27 14:58:08 CST 2004

Hi Andy

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

Cheers - Peter

"Andy Bowles" <andy.bowles@virgin.net> wrote in news:eWWD36Q5DHA.2412
@TK2MSFTNGP11.phx.gbl:

> "Peter Hewett" <Nospam@xtra.co.nz> wrote:
>
>> 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
>
>
>