M
mccaskey
Is there a way to test whether a Range (or Selection) is in a Field,
and if so, to access that field?
and if so, to access that field?
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 MVPwww.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petitionwww.classicvb.org
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?
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
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
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
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.