Identify Active Formfield Item Number?

P

Paul J

I am creating a locked Word 2003 document containing a number of
(un-bookmarked) Text Form Fields.

Owing to a problem with the limited availability of number formats for the
Text Form Field (see previous Discussion Group question), I need to run a
Macro on exiting the field to reformat the Result. The Macro will something
like the following:

Sub FieldFormatter()
With ActiveDocument
Dim X as Integer ' Item number of the Text Form Field that ran the
Macro
X = ?????????????????????????????????? ' WHAT? HELP!
.FormFields(X).Result = Format(.FormFields(X).Result, "£#,##0")
End With
End Sub

Can anyone please advise me as to how I can identify the Item number for the
FormField (Text Form Field) that called the Macro?

All contributions will be gladly accepted!

Paul J
 
B

Bear

Paul:

If you're talking about formfields created using the Forms, then they must
have a bookmark. If you open the Text Form Field Options dialog box for any
text formfield, you'll find a Bookmark option with *some* value. The
important thing is to assign meaningful names to these bookmarks.

Here's some code that shows how to determine the formfield that the user
just exited from, and uses a case structure to allow different processing for
each field.

~~~~~
Dim strCurrBookmark as String

If Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
strCurrBookmark = Selection.Bookmarks(Selection.Bookmarks.Count).Name
Else
strCurrBookmark = Selection.FormFields(1).Name
End If

Select Case strCurrBookmark
Case "txtAlpha"
If ActiveDocument.FormFields("ckVia").CheckBox.Value = True Then
' If checked, enable "other" field
ActiveDocument.FormFields("bkVia").Enabled = True
Else
' If not checked, clear and disable "other" field
ActiveDocument.FormFields("bkVia").DropDown.Value = 1
ActiveDocument.FormFields("bkVia").Enabled = False
End If
Case "txtBeta"
' Processing for Beta
Case "txtGamma"
' Processing for Gamma
Case Else
' Failsafe should you forget a field
End Select

End Sub
 
D

David Sisson

Sub WhichFF()

Select Case Selection.BookmarkID
Case 1
'Apply this format

Case 2
'Apply that format

End Select

End Sub
 
P

Paul J

Gentlemen (Bear and David),

Thanks for your responses.

Bear states that the FormFields MUST be bookmarked and that would hold true
for his elegant solution.

However, my FormFields are de-bookmarked by Word in the course of creating
my form document (duplicate bookmarks are automatically cleared).

So my problem remains: how can I identify the Item number of the
un-bookmarked FormField that invokes the Macro?

Paul J
 
D

David Sisson

When a document is opened, Word counts all the collections.
Formfields are one of those collections.

In the formfields in question, select the WhichFF as the Exit Macro.
The collection number is identified by the BookmarkID property.

To reitterate:

Sub WhichFF()

Select Case Selection.BookmarkID
Case 1
'This is Bookmark 1

Case 2
'This is Bookmark 2

End Select

End Sub

Keep in mind, that if a new formfields is added, the count in the
Select Case will be off.

It's best to take the time and name all the formfields.
 

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