Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Identify Active Formfield Item Number?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul J - 26 Jun 2007 12:33 GMT
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
Bear - 26 Jun 2007 14:36 GMT
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
Signature

Windows XP, Word 2000

David Sisson - 26 Jun 2007 15:06 GMT
Sub WhichFF()

Select Case Selection.BookmarkID
   Case 1
       'Apply this format

   Case 2
       'Apply that format

End Select

End Sub
Paul J - 27 Jun 2007 10:58 GMT
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

> Sub WhichFF()
>
[quoted text clipped - 8 lines]
>
> End Sub
David Sisson - 27 Jun 2007 14:37 GMT
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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.