Is range in a field?

M

mccaskey

Is there a way to test whether a Range (or Selection) is in a Field,
and if so, to access that field?
 
J

Jonathan West

mccaskey said:
Is there a way to test whether a Range (or Selection) is in a Field,
and if so, to access that field?

If Selection.Fields.Count > 0 Then
' selection is in a field or contains a field
MsgBox Selection.Fields(1).Code ' show the code of the first field

Else
' no fields about
End If


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

mccaskey

If Selection.Fields.Count > 0 Then
' selection is in a field or contains a field
MsgBox Selection.Fields(1).Code ' show the code of the first field

Else
' no fields about
End If

--
Regards
Jonathan West - Word MVPwww.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petitionwww.classicvb.org

Tried that, and it didn't work for me (Word 2003 on XP). It behaves as
you describe if the selection spans the entire field, but if the
selection is within the field (in either its code when I'm looking at
code or results when looking at results), then .Count returns 0.
Accessing Fields(1) returns a 5941 error, "Requested member of
collection does not exist".

Does it work for you?

Thanks,
John
 
J

Jonathan West

mccaskey said:
On May 16, 8:29 am, "Jonathan West" <[email protected]> wrote:
Tried that, and it didn't work for me (Word 2003 on XP). It behaves as
you describe if the selection spans the entire field, but if the
selection is within the field (in either its code when I'm looking at
code or results when looking at results), then .Count returns 0.
Accessing Fields(1) returns a 5941 error, "Requested member of
collection does not exist".

Does it work for you?


Ah, misunderstood you. Try this instead.

Dim iField As Long
iField = ActiveDocument.Range(0, Selection.End).Fields.Count
If iField > 0 Then
If Selection.InRange(ActiveDocument.Fields(iField).Result) Then
MsgBox ActiveDocument.Fields(iField).Code
End If
End If
 
J

Jean-Guy Marcil

mccaskey was telling us:
mccaskey nous racontait que :
Tried that, and it didn't work for me (Word 2003 on XP). It behaves as
you describe if the selection spans the entire field, but if the
selection is within the field (in either its code when I'm looking at
code or results when looking at results), then .Count returns 0.
Accessing Fields(1) returns a 5941 error, "Requested member of
collection does not exist".

Does it work for you?

No.

It is a bit more complicated if the selection is within a field.
Try this sub that calls a function that does the work:

'_______________________________________
Sub Test()
Dim fldCurrent As Field

Set fldCurrent = SelectionContainsAField

If Not fldCurrent Is Nothing Then
'Show the code of the first field
MsgBox fldCurrent.Code
Else
MsgBox "No fields in the current selection."
End If

End Sub
'_______________________________________

'_______________________________________
Public Function SelectionContainsAField() As Field
Dim rngSel As Range
Dim fldFirst As Field

' Nothing special to do if the selection contains a field
If Selection.Fields.Count > 0 Then
Set SelectionContainsAField = Selection.Fields(1)
Exit Function
End If

'If not, expand range
Set rngSel = Selection.Range
rngSel.Expand wdParagraph


'Check if selection is really in a field and that we are _
not picking up a field from the expanded parqagraph
For Each fldFirst In rngSel.Fields
If Selection.InRange(fldFirst.Result) Then
Set SelectionContainsAField = fldFirst
Exit For
End If
Next

End Function
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Jonathan West was telling us:
Jonathan West nous racontait que :
Ah, misunderstood you. Try this instead.

Dim iField As Long
iField = ActiveDocument.Range(0, Selection.End).Fields.Count
If iField > 0 Then
If Selection.InRange(ActiveDocument.Fields(iField).Result) Then
MsgBox ActiveDocument.Fields(iField).Code
End If
End If

Much simpler!

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Jonathan,

not bad at all.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jean-Guy Marcil

Jonathan West was telling us:
Jonathan West nous racontait que :
Ah, misunderstood you. Try this instead.

Dim iField As Long
iField = ActiveDocument.Range(0, Selection.End).Fields.Count
If iField > 0 Then
If Selection.InRange(ActiveDocument.Fields(iField).Result) Then
MsgBox ActiveDocument.Fields(iField).Code
End If
End If

I had a thought... I thought that your code would somehow get the last field
in the selection and that mine would get the first one.

So I retested your code and realized that if the current selection was a
range that included a field (or fields) somewhere inside (i.e.
field.Range.Start was higher than Selection.Range Start and field.Range.End
was smaller than Selection.Range.End), then your code returned nothing.

So I modified it as follows:

'_______________________________________
Dim iField As Long

If Selection.Fields.Count > 0 Then
'Get the first field
MsgBox Selection.Fields(1).Code
Else
iField = ActiveDocument.Range(0, Selection.End).Fields.Count
If iField > 0 Then
If Selection.InRange(ActiveDocument.Fields(iField).Result) Then
MsgBox ActiveDocument.Fields(iField).Code
Else
MsgBox "The current selection does not contain any fields."
End If
Else
MsgBox "There are no fields in this document"
End If
End If
'_______________________________________


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

mccaskey

Jonathan West was telling us:
Jonathan West nous racontait que :






I had a thought... I thought that your code would somehow get the last field
in the selection and that mine would get the first one.

So I retested your code and realized that if the current selection was a
range that included a field (or fields) somewhere inside (i.e.
field.Range.Start was higher than Selection.Range Start and field.Range.End
was smaller than Selection.Range.End), then your code returned nothing.

So I modified it as follows:

'_______________________________________
Dim iField As Long

If Selection.Fields.Count > 0 Then
'Get the first field
MsgBox Selection.Fields(1).Code
Else
iField = ActiveDocument.Range(0, Selection.End).Fields.Count
If iField > 0 Then
If Selection.InRange(ActiveDocument.Fields(iField).Result) Then
MsgBox ActiveDocument.Fields(iField).Code
Else
MsgBox "The current selection does not contain any fields."
End If
Else
MsgBox "There are no fields in this document"
End If
End If
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org

Thanks!
 

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