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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ms word2000 - how to find out the index of the formfield that called

Status
Not open for further replies.

cristimitulescu

Technical User
May 14, 2004
4
GB
Hi,
I am trying to create a word document where users type in some numbers in a Text Form Field and on exit of the Form Field a second text Form Field gets the same value as the first Form Field. This was eassily achieved with the code

Public Sub sta()
ActiveDocument.FormFields("aa1").Result = ActiveDocument.FormFields("a1").Result
End Sub

a1 is the name of the first Text Form Field and a2 is the second Text Form Field . These 2 lie in a row in a table with hundreds of rows.
The problem is that I have a few hundred of these fields displayed in a table and naming each one of these individually and creating a Sub for each row would be super time consuming.
I was thinking that if there was a way to create only one Sub to do the copying of contents by finding out what index form field had called the sub and using that to calculate where to copy to.
Unfortunatelly I have no clue where to start looking.
Thank you.
 
Could you describe the logic in a little more detail? It would be fairly simple to use the result of one formfield to fill in ALL other formfields with the same result. Just loop through all the formfields and make the results equal.

However, if you have hundreds, and only, say, two, will take the same result then they have to be identified specifically.

If some of your hundreds can be grouped logically, then you could - sorry - name them sequencially. E.g. Finance1 to Finance36. In this case you could take a result and make the code only loop through the formfield names with a string and a number, setting the results according to the initial name.

This does make you name all your formfields. I am afraid that this is generally speaking good practice anyway, for exactly the reason you posted. The formfield collection index number is dynamic. If you have 100 formfields and you get the index number of the second one, then it is formfields(2). However, if you select 20 formfields in the middle of the document, the second one, of the selection is formfields(2). This is why if you select ANY one formfield the index is always (1). And also why if you have a formfield with an index number of 4, and you move that formfield to be the first in line - the index is now 1.

The index number only reflects the order in which the formfield appears, by document, range, or selection. It is not fixed to the field itself.

The only sure way to identify a specific formfield is by name.

What are the names now - Text1...Textn?

The only other suggestion is using the formfield Next property. This would work best if the the ff getting the result are in sequence.

Gerry
 
The forms are always related numerically so for example the destination form field is always 9 form fields away from the source which is why indexes could work.
Any thoughts on how a noob like me could get the index and then increase the index by 9.
I have to update the destination field on an individual basis and not at the end of filling in all the source form Fields
 
Any one formfield does NOT have an index number assigned to it. Well it does, but it is dynamic. It depends on where it is in order - and that order depends on the range considered for indexing.

Say you have 10 formfields. The index number of, say, the third from the last is:

Formfields(8) considering the whole document

Formfields(1) considering a selection that start just before it , going to the end.

Formfields(3) considering a range set for before the fifth formfield (now formfields(1)) to the 7th formfield.

So what is the index number? As shown, it depends. Therefore, there is no method to extract the index number; and therefore no way to add or subtract from it easily.

This is why good design always names formfields, at least with some sort of group and a sequence number.

You did not answer my question. How are the formfields named now? If they are using the default Word naming (i.e. Text1, Text2...), there is a rather kludgey solution. It is possible to have a macro that uses temporary bookmaks to set a range covering a exiting formfield. Using a selection.goto jump forward 9 fields, set another temporary bookmark, reset the range to cover those fields. Then you could get the result of the current index (1) and dump it into the current index(9).

The alternative is to use your finger and count through the document, noting the order count for each formfield. Then for each field that its result jumps nine more fields, write a sub that will do that.

To sum up, the answer to your question of getting the index number and increasing by nine - is that it is not practical. I am working on an improvement of the kludgey way. When (if) I get it neat enough to post, I will post here.



Gerry
 
? From the Word MVPs:

If you need to know which formfield the user tabbed out of in a macro you're using as on-exit macro you need to take into account that you can't always simply use the command Selection.Formfields(1).Name. This line won't work with a textformfield although it works with checkboxes and dropdowns.

If you need code to find out which formfield was tabbed out of, that works with every type of formfield, use:

If Selection.FormFields.Count = 1 Then
'No textbox but a check- or listbox
MsgBox Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If

I'm probably way off, right? :)

Anne Troy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top