PDA

View Full Version : Solved: Retrieving FormField Indices dynamically



cunnus88
06-24-2005, 05:37 AM
I am designing a document with a lot of repetitive formfields.

I have configured each formfield to call a macro after the user has selected an entry from the list.

What this macro should do is change the list entries in the next formfield depending on what the user selected in the present formfield.

Now I've just discovered that

selection.formfields.item

is meaningless since "item" or "formfields" requires an index in parentheses to work but the index is what I'm trying to find out in the first place!

Is there a way in VBA to return the index value of a formfield which has just been changed?

Something like this event:
Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
provided in excel would be VERY helpful, where the current formfield could be a parameter just like the "Target" above.

Thanks a lot in advance.

fumei
06-24-2005, 01:27 PM
1. Clarify what you mean by repetitive formfields. Do you mean the VALUE is repeated? If so, consider using a Field, rather than a formfield.

2. This is a very good example of why you should always explicitly name formfields. Then you can refer by name, as in ActiveDocument.FormFields("CustomerName").

3. The index is always the order the formfield appears in the document. The index is used to set the default name. So if you have Text1, Text2, Text3...and you move Text2 to before Text1, it will BECOME Text1. Its index changes, but its name changes too!! So any code that specifies "Text2" is actually specifying "the SECOND formfield textbox in this document".

ActiveDocument.FormFields("ThisBox") will always refer to THAT formfield, regardless of where it may be moved to.

So, what exactly are you trying to do?

cunnus88
06-24-2005, 04:14 PM
Thanks for the reply.

But I got the solution from somewhere else.

It seems that selection.formfields(1) will always return the first formfield within the selection.

And it was just what I was looking for.

TonyJollans
06-25-2005, 01:50 AM
Hi cunnus88,

Welcome to VBAX!

Using (1) always returns the first member of a collection - if that collection has any members.

A word of caution, however. The Selection in a protected Word Form is a funny beast - it can only contain one formfield because you can't select multiple ones but it depends on the type of FormField whether you even get that one.

When entering or exiting a Dropdown the Selection.FormFields collection contains the dropdown and so you get what you want in your case.When entering or exiting a Text FormField, however, the Selection.FormFields collection does not contain anything and the code would fail in the general case.

MOS MASTER
06-25-2005, 11:56 AM
Hi and Welcome to VBAX! :hi:

Tony is absolutly right.
If you are in a protected form and you wish to retrieve the first formfield in selection you should not use:
MsgBox Selection.FormFields(1).Name


Instead use:
MsgBox Selection.Bookmarks(1).Name

to get a handle on the first formfield in selection.

Enjoy! :whistle: