field problem

K

keith kuan

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!
 
C

Charles Kenyon

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 said:
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?!
 

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