Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I determine the name of the form field that I just exited

Status
Not open for further replies.

barnard90

IS-IT--Management
Mar 6, 2005
73
US
Hi

I have a document with a lot of form fields in it
On the exit any form field , I need to determine the name and text result of any form field

I need to have a message box displaying the name and text of the form field that I just exited

How do I write a macro for that

Please suggest

thanks
 
barnard90,
do you have any code yet?
if not i would start with the macro recorder (tools -> macro -> record neew macro). fill in and exit a couple of fields. stop the macro recording and take a look at the code.
then post your specific questions.
regards,
longhair
 
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
 


Hi,

Have a Prev and This variables

In the Entry Macro you assign...
Code:
Prev = This
This = ActiveFormFieldName 'whatever the syntax Gerry knows!
Then you always know the Prev form name.

Skip,

[glasses] [red][/red]
[tongue]
 
fumei,
can one not take a copy of the doc and unprotect it to work with? i would not think that test coding an active app would be a good idea (^o^).
anywho, the suggestion was to get some base code and then assign the data to vars for later usage (like SkipVought) pointed out.
regards,
longhair
 
The point is - there IS no ActiveFormFieldName!

The OnEntry macro fires on entry...not after entry. So, in fact, the formfield has NOT been actually entered. Any OnEntry macro will NOT be able to directly get any information about the formfield it is entering. Even if it IS entered, again, while it looks like the formfield is selected, in fact it is not.

If the document is UNprotected, and you select the formfield, then yes
Code:
Selection.Formfields(1).Name
will return the formfield name. But if the document is protected, no, it won't.

Here is my inelegant code to get formfield names, while protected. As stated, you have to unprotect first and fiddle.
Code:
Function ExitedFormfieldName() As String
Dim r As Word.Range
ActiveDocument.Unprotect
Set r = ActiveDocument.Range(Start:=0, End:=Selection.Range.Start)
[COLOR=red]' gets the name of the LAST formfield
' in the range ie. the one just exited[/color red]
ExitedFormfieldName = _
    ActiveDocument.FormFields(r.FormFields.Count).Name
ActiveDocument.Protect wdAllowOnlyFormFields, NoReSet:=True
Set r = Nothing
End Function
So if you put the following as the OnExit macro, you will get the result of exiting (text) formfield.
Code:
Sub WhereWasI()
MsgBox ActiveDocument.FormFields(ExitedFormfieldName).Result
End Sub

Gerry
My paintings and sculpture
 
Obviously, if you want to really error trap this you would be checking for the TYPE of formfield. The function returns only the name of the last formfield. If the last formfield is a checkbox, then .Result will give an error. And if it is a dropdown, then you will need to use the .ListEntries property of the dropdown.

Gerry
My paintings and sculpture
 
I have GOT to ask a question:

Having just entered data in a field, why in the world do you need to have a message box display the name of the field and the text that you just entered? And do this for each of the "many" fields you have. So you enter your data, the message box pops up, then you have to go to your mouse to click on the message box to clear it. Then you move to your next field. I got to tell you, this is the antithesis of user friendly!


The Missinglinq

There's ALWAYS more than one way to skin a cat!
 
Oh....well, in the real world of COURSE you would never do that! Sheeesh. And yes, the original poster DID in fact say:
barnard90 said:
I need to have a message box displaying the name and text of the form field that I just exited
I was sort of ignoring that part.

Why? Because there certainly CAN be reasons for finding out the value/result of a formfield just exited. So I was following up that aspect, because this is a odd part of the Word object model.

HOWEVER, yes, I totally agree - displaying a messagebox coming out of a formfield is not very user friendly at all. After all...the user can simply SEE the result right there.

But just to be a devil's advocate....when a document is protected for forms NONE of the document in the document is available for editing. This includes viewing. You can NOT scroll and look at a protected part of the document UNLESS that part has a formfield in it. The focus jumps from formfield to formfield.

So. Say you have a formfield on page 2, then the next one is one page 4. On exiting the one on page 2, the focus will immediately jump to page 4. As the a courtesy to the user, it may be legitimate to display what they put in the last formfield.

A stretch, I know.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top