This is not as easy as you may think.
longhair - the macro recorder is not available when forms are protected. In other words, your suggestion to use the macro recorder and fill in and exit a couple of fields, is not an option.
OnExit fires when you exit a formfield. It is important to note that the "Selection" object HAS exited the formfield. Therefore:
Selection.Formfields(1).Name will return an error - the object does not exist. The Selection is not in a formfield.
Further, assuming there IS another formfield the "Selection" is going
into, it has not got there yet. So it still in not in a formfield.
In fact, this is a very odd part of the Word object model. If you have a formfield selected - ready to type in something - and try to query its name...you can't. There IS a Selection.Formfields collection. But......with the document protected for forms, and a formfield "selected" (that is, it seems to be highlighted), the following will ALWAYS = 0.
Code:
Selection.FormFields.Count
There is no formfield within the selection.
Weird huh?
More. Make a new document. Put in a text formfield - don't do anything with it. Run the follow:
Code:
Sub FormFieldStuff()
MsgBox "Formfield RANGE: " & _
ActiveDocument.FormFields(1).Range.Start & " - " & _
ActiveDocument.FormFields(1).Range.End & vbCrLf & _
vbCrLf & _
"Selection RANGE: " & Selection.Range.Start & " - " & Selection.Range.End
End Sub
You get:
Formfield RANGE: 0 - 19
Selection RANGE: 19 - 19
The Selection number is OK, it is at the end of the formfield, and is only one character. The formfield numbers are a bit odd. However, ignore that for now.
Now protect it for forms. Remember there is nothing else in the document. just the formfield. As soon as you protect it for forms, that formfield is "selected". Don't type anything in, leave it blank. Run the code again. Here is what you get:
Formfield RANGE: 0 - 19
Selection RANGE: 13 - 18
Even though the formfield SEEMS to be selected, according to the object model, the Selection object does NOT include both Start and End of the Range, and therefore does NOT have anything in the Selection Formfield collection.
Bottom line...I would absolutely LOVE to hear someone post a efficient way to get:
1. the current formfield name/result WITHOUT using the explicit index or name. This seems obvious - if you HAVE the explicit index or name...getting the name using that information is silly.
2. what the OG is asking - information on the exited formfield. Again, without using explicit index or name.
You can get that explicit data, but you have to unprotect, and do very inelegant things to get it.
Gerry
My paintings and sculpture