ms word2000 - how to find out the index of the Text FormField that called the Sub

C

cristi

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 easily
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.
 
P

Peter Hewett

Hi cristi

It's pretty simple really, try this code:

Public Sub UpdateAnotherFF()
Dim strThisFF As String

strThisFF = CurrentFFName
With ActiveDocument.FormFields
.Item("a" & strThisFF).Result = .Item(strThisFF).Result
End With
End Sub

Private Function CurrentFFName() As String
With Selection
If .FormFields.Count = 1 Then

' CheckBox or DropDown, with or without a name
CurrentFFName = .FormFields(1).Name

' Only executed if the TextBox has a name
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
CurrentFFName = .Bookmarks(.Bookmarks.Count).Name
End If
End With
End Function

In the above example it assumes the FormField you are assigning the value to has the same
name as the current FormField but with an "a" prefix. So in this case if the FormField
you are leaving is named "a1" it assigns the value to another FormField named "aa1".
Obviously for the code to work the pairs of FormFields require the same name apart from
the prefix character used to designate the FormField you're assigning a value to.

Just hook the "UpdateAnotherFF" macro up as the OnExit macro of the FormFields you want to
use to assign the value of. So in the above example the macro would be the OnExit macro
of the FormField "a1".

HTH + Cheers - Peter
 
C

cristi

Thank you for your reply but with my limited knowledge of VBA I convinced
myself that your code only works with Text Form Fields that have been
named. This saves me half the time which is great but no harm in asking if
there is a way for me not to name the hundreds of fields in order to
reference them in the sub. Something like refering to them by an index
number which I would get from whatever generated the event. Sorry if I
sound very ignorant and I may be asking for things that might not make
sense in VBA.
 
P

Peter Hewett

Hi cristi

Even though you may be able to do what you want using derived Index numbers I'd still use
the names, it may take a tad longer to set up but it aint gonna break.

The caveats of using an index number are as follows. The order of FormFields in a table
is left to right (across a row), top to bottom (down to the leftmost FormField in the next
or subsequent row). For this to work your Source and Destination FormFields
(DestinationFF = SourceFF) must have a fixed relationship to each other. In other words
one must *always* be plus or minus the same number of FormFields away from each other
throughout the table. If the two are always adjacent or there is a set number of
intervening FormFields then it will work, otherwise not.

HTH + Cheers - Peter
 
C

cristi

My source and destination fields always have the same relation. +9 I
think(this index only counts form fields right?)
I modifyed the code you kindly gave me but I do not know how to increase the
Index. I keep on getting type missmatch

Sub sta1()
With Selection
CurrentFFName=.Bookmarks(.Bookmarks.Count).Name
End With

NewFFName=CurrentFFName+1
ActiveDocument.FormFields(CurrentFFName).Result=ActiveDocument.FormFields(NewFFName).Result

End Sub
 
P

Peter Hewett

Hi cristi

It's no longer simple. The code I though would work did not, but eventually I came up
with something that did:

Public Sub UpdateAnotherFF()
Dim lngFFIndex As Long

lngFFIndex = CurrentFFIndex()
If lngFFIndex > 0 Then
With ActiveDocument.FormFields

' One of these should work
.Item(lngFFIndex + 9).Result = .Item(lngFFIndex).Result
'.Item(lngFFIndex - 9).Result = .Item(lngFFIndex).Result
End With
End If
End Sub

Private Function CurrentFFIndex() As Long
Dim ffItem As Word.FormField
Dim ffToFind As Word.FormField
Dim lngFFIndex As Long
Dim strCurrentFFName As String

With Selection
If .FormFields.Count = 1 Then

' CheckBox or DropDown, with or without a name
Set ffToFind = .FormFields(1)

' Only executed if the TextBox has a name
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
strCurrentFFName = .Bookmarks(.Bookmarks.Count).Name
Set ffToFind = ActiveDocument.FormFields(strCurrentFFName)
End If
End With

For Each ffItem In ActiveDocument.FormFields
lngFFIndex = lngFFIndex + 1
If ffItem.Range.Start = ffToFind.Range.Start Then
CurrentFFIndex = lngFFIndex
Exit Function
End If
Next
End Function

HTH + Cheers - Peter
 
C

cristi

It was a long shot asking(the laziness in me) ;-)
I ended up using your initial code and also lost my vision partially naming
hundreds of the fields but the task is achieved. Thanks again for your
help.
 
P

Peter Hewett

Hi cristi

Glad you get there in the end!

Cheers - Peter

It was a long shot asking(the laziness in me) ;-)
I ended up using your initial code and also lost my vision partially naming
hundreds of the fields but the task is achieved. Thanks again for your
help.

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top