Consulting

Results 1 to 5 of 5

Thread: Solved: Retrieving FormField Indices dynamically

  1. #1

    Solved: Retrieving FormField Indices dynamically

    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.
    Last edited by cunnus88; 06-24-2005 at 05:41 AM. Reason: typos

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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?

  3. #3
    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.

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi and Welcome to VBAX!

    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: [VBA]
    MsgBox Selection.FormFields(1).Name
    [/VBa]

    Instead use:[VBA]
    MsgBox Selection.Bookmarks(1).Name
    [/VBA]
    to get a handle on the first formfield in selection.

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •