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

Word select form field

Status
Not open for further replies.

JudyL

Instructor
Feb 21, 2001
148
US
I need a statement in a Word macro that will select a form field in a locked document. I have a macro that will check the length of the field and if it is zero length (meaning that the person did not enter anything in the field), it displays an error message. That part works.

Then I want it to reselect the field but it just goes on to the next field.
 
Hi Judy,

Try this:
Code:
Sub Test()
ActiveDocument.Unprotect Password:="HI"

If ActiveDocument.FormFields("Text1").Result = vbNullString Then
ActiveDocument.FormFields("Text1").Select
Else
ActiveDocument.FormFields("Text2").Select
End If
ActiveDocument.Protect Password:="HI", NoReset:=False, Type:= _
wdAllowOnlyFormFields

End Sub

Enjoy,
Joost Verdaasdonk
 
I don't understand why I should need to unprotect the document but I did as you suggested but as soon as the macro finished it went to the next field and selected it. I had set it up to run upon exiting the first text field. I tried stepping through the code and watched it as it did each step. It activated as planned as soon as a exited the field by tabbing but the field stayed selected until it executed the end sub. Then it went on to the next field.
 
Try this. Put it as the Entrance macro to the NEXT field, not the exit macro to the field you are trying to check. I would recommend, if you have a number of formfields, to use a UserForm instead. That way you can do all your error trapping on one form, and dump the correct (and only correct) information into your formfields, or bookmarks, or whatever. In any case this does not require unprotecting, and does set focus back to the offending formfield.

Code:
Sub CheckPreviousFieldContent()
    ' or use Result = vbNullString; or some other error checking
    ' like .Result = "Blah blah Co."
If ActiveDocument.FormFields("Text1").Result = "" Then
    MsgBox "Invalid input in the last field.  Please input again."
    Selection.GoTo What:=wdGoToBookmark, Name:="Text1"
Else
    Exit Sub
End If
End Sub

Hope this helps.



Gerry
 
Thanks for the advice. This is for a client who wants to use Form Fields and they want to be able to sometimes point and click on fields to fill the form in random order. Having the macro in the next field as an entrance macro is not going to work for them.

Any other ideas?
 
In that case, I would strongly recommend making it a UserForm. Then they can play with the field information as much as they like. You could even use MultiPages on the UsrForm. That always seems to impresses people who like to input things in random order.

In any case, if that is a requirement (and I have to say a not uncommon, although...oh, never mind), then a UserForm is definitely the way to go. They could do it completely randomly, because it would only be filled when they click the OK button. Just make sure that your error trapping is tight.

You could even do it so that it fires on start up; lets them do their thing; dumps the (error trapped) input into their formfields; with an entrance, and exit macro that loads the UserForm again.

Those macros (both the same of course) could load the current information (error trapped for blanks - so it would work on first pass as well). That way, they could play with it empty (first pass), or with whatever they have already put in (first pass + X). I would do this with an document variable ("Done") that would be set if they have already input stuff.

Even better - if they have put in data, and they enter a formfield again (to change it) put up a question box that asks if they want to a) just change that field, or b) others as well. If a) use an input box to grab the new information and dump it into the formfield. If b) reopen the full UserForm and let 'em play with them all.

There are all sorts of solutions to your situation.

Hope this helps.


Gerry
 
Gerry, thanks for your help but they really want it to just be a regular Word form. They have lots of these but not the expertise to create user forms and they don't want to pay me to create that many. They just want to make the field a required field. I guess I don't understand why the macro can't go to the field. What is causing it to move on when the macro is done?
 
The reason it moves is that formfields are designed to do precisely that. Move, in order of fields through the locked (protected) sections.

You will have to analyze your requirements.

You want to be able to randomly choose the field. OK, what if the user randomly chooses not to put anything? This is essentially the original post.

Where are you going to check for this? You need an event that will fire your checking. Where were you going to run this? I put it as the entrance macro for the next formfield because that is what Word does with formfields once they are filled in - move to the next field.

Essentially though, the code you need will be based on a variant of:

If aDoc.FormFields("text3").Result = "" Then
aDoc.Bookmarks("text3").Select

Why don't you post your macro code. I am sure we can make it work in a manner that is viable.


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top