Hi, i have a tricky problem in word's selection.field, here is my code to generate fields

Selection.Fields.Add
Selection.Range,
wdFieldEmpty,
Text,
Fals

if i have 5 fields in the word document, and i want to select these fields and read the details of the fields by a loop, how can i do it?

i can get like: selection.fields.count =
but i don't know how to read it one by one...

Thanks very much for your help!

Re: field problem by Charles

Charles
Sat Feb 21 12:39:09 CST 2004

Selection.Fields is only the fields in the current selection.
ActiveDocument.Fields will pick up fields in the body of your document (but
not those in headers/footers).

What follows is Peter Hewett's response on 2/20/04 in the thread "VBA Word"
for code to update fields including those in headers/footers. It may help
you if yours are not in the main body.

Hi Ny

This VBA code will update all fields in your document, including the headers
and footers:

Public Sub UpdateAllFields()
Dim lngJunk As Long
Dim rngStory As Word.Range

' Word missing first Header/Footer bug workaround
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType

' Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges

' Iterate through all linked stories
Do
' Do Update all fields
rngStory.Fields.Update

' Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub

HTH + Cheers - Peter

To look at each field, consider

Dim oField as Field
For Each oField in rngStory
MsgBox oField.text
Next oField

Don't know about oField.text; might be oField.value.

--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

"keith kuan" <eekeith@inesc-macau.org.mo> wrote in message
news:E204F6F0-6EBE-464A-A355-D99E914903FD@microsoft.com...
> Hi, i have a tricky problem in word's selection.field, here is my code to
generate fields:
>
> Selection.Fields.Add _
> Selection.Range, _
> wdFieldEmpty, _
> Text, _
> False
>
> if i have 5 fields in the word document, and i want to select these fields
and read the details of the fields by a loop, how can i do it?!
>
> i can get like: selection.fields.count
= 5
> but i don't know how to read it one by one....
>
> Thanks very much for your help!