Reselect a FormField following edit

T

Tait Milliken

I have a Word 2000 document with several FormFields. These
fields will ultimately be used to populate an Access
database after several documents have been created from
the template. Since the loading of the Access db takes
place separately, I need to perform as much editing as
possible within the Word document. I have written several
macros that are invoked on exit from most FormFields where
editing is needed. My problem is that I cannot get the
FormField that had the error to be reselected. Consider
the following code:

Sub Validate_ITLExtension()
'
' This macro ensures that the ITL telphone extension is
five digits long with
' a leading "3"
'
Dim szExtension As String

szExtension = ActiveDocument.FormFields
("txtITLExt").Result
If Not (Len(szExtension) = 5 And Mid$(szExtension, 1, 1)
= "3") Then
MsgBox "Extensions must be five digits including a
leading ""3""", vbOKOnly And vbExclamation, "Telephone
Extension Error"
ActiveDocument.FormFields("txtITLExt").Result = ""
ActiveDocument.Bookmarks("txtITLExt").Select
End If

End Sub

The errors are properly trapped but the .select is
apparently ignored and the next FormField is activated.

I have tried using

Selection.GoTo What:=wdGoToBookmark, Name="txtITLExt"

but it doesn't work either.

Suggestions?
 
P

Peter Jamieson

I expect there's a correct way to do this but something that may work is to
store the form field number in the exit macro if you want to go back to it,
then ensure each entry macro checks for a non-zero number and selects the
appropriate field, e.g. with two formfields:

Private FormFieldNumber As Long
Sub ExitMacro1()
With ActiveDocument
If .FormFields(1).Result = "abc" Then
.FormFields(1).Result = ""
FormFieldNumber = 1
Else
FormFieldNumber = 0
End If
End With
End Sub

Sub b()
With ActiveDocument
If .FormFields(2).Result = "abc" Then
.FormFields(2).Result = ""
FormFieldNumber = 2
Else
FormFieldNumber = 0
End If
End With
End Sub

Sub e()
Dim TempFormFieldNumber As Long
If FormFieldNumber <> 0 Then
' can probably just put the FormFieldNumber = 0 after the .Select but it
' feels better this way
TempFormFieldNumber = FormFieldNumber
FormFieldNumber = 0
ActiveDocument.FormFields(TempFormFieldNumber).Select
End If
End Sub

Untested, but maybe worth a try.
 
C

Cindy M -WordMVP-

Hi Peter,
I expect there's a correct way to do this but something that may work is to
store the form field number in the exit macro if you want to go back to it,
then ensure each entry macro checks for a non-zero number and selects the
appropriate field, e.g. with two formfields:
This is also my preference, except I store the actual form field name - on
ENTERING the field. And the Exit macro performs the validation on the field
its exiting.

I believe there's also an article on this on the mvps.org/word website.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
T

Tait Milliken

Thank you, Peter and Cindy:

Your recommended solution works exactly as desired.

I will next generalize the edits to cut down on the number
of subsoutines. Your suggestions point the way. Thanks
 

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