"IsInField" method?

E

Ed

Is there a "WithinField" type method to determine if a collapsed cursor is
within a field?

I want to be able to determine if my cursor is in a field and then determine
certain values of the field.

There are ways to determine if a cursor is in a table
(Selection.Information(wdWithInTable)) but I cannot find a similar technique
for a field. (I tried A = Selection.Field(1) when the cursor was in a field,
but it errored out. Unlike the 'table' method, unless the entire field is
highlighted, VBA doesn't seem to know that it is in a field.)

Is there a way?

--Ed (in Virginia)
 
G

Graham Mayor

Wouldn't it be simpler to just locate the field you want and read the
information from it? Perhaps if you were to supply more information about
what you are trying to do. Which Word version? What sort of field? Where is
it likely to be in the Document? We may be able to help e.g. the following
will read the code and result of the 1st field in the 1st paragraph

Dim sResult, sCode As String
With ActiveDocument.Paragraphs(1).Range.Fields(1)
sResult = .Result
sCode = .Code
End With
MsgBox sCode & vbCr & sResult


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Ed

Graham,

I don't want to know what is IN the field at the start of the process so
much as I want to know if I am in one. If the cursor is in a field, I want
to expand it to capture the entire field and then capture the ".result" and
the ".code" as you suggest in your answer. But if the cursor is not in a
field, I need to do something else. (Not dissimilar to the way certain
'selection' actions are called with one is within a table vs. outside of a
table.)

That's why I am looking for the "WithInField" function.

--Ed (in Virginia)
 
E

Ed

Graham,

I don't initially want to know what is IN the field at the start of the
process so
much as I want to know if I am actually in one. If the cursor is in a field,
I want
to expand it to capture the entire field and then capture the ".result" and
the ".code" as you suggest in your answer. But if the cursor is not in a
field, I need to do something else. (Not dissimilar to the way certain
'selection' actions are called with one is within a table vs. outside of a
table.)

That's why I am looking for the "WithInField" function.

--Ed (in Virginia)
 
D

Doug Robbins - Word MVP

Application.ScreenUpdating = False
Dim myrange As Range
Set myrange = Selection.Range
Dim afield As Field
Dim Flag As Boolean
Flag = False
For Each afield In ActiveDocument.Fields
afield.Select
If myrange.Start >= Selection.Start And myrange.End <= Selection.End
Then
Flag = True
MsgBox "in Field"
End If
Next afield
If Flag = False Then
MsgBox "Not in Field"
End If
myrange.Select
Application.ScreenUpdating = True


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
T

Tony Jollans

Function InField() As Boolean
With ActiveDocument
InField = 0 <> .Range.Fields.Count - _
.Range(.Range.Start, Selection.Start).Fields.Count -
_
.Range(Selection.End, .Range.End).Fields.Count
End With
End Function
 

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