PDA

View Full Version : Word VBA Required Fields or On Save Event help



MBGoodwin
09-28-2010, 03:04 PM
Version of the program WORD 2007
What you want it to do SET FIELDS TO REQUIRED ON A WORD USER FORM USING VBA
Cell references, bookmark names, column letters, row numbers, worksheets, styles, whatever pertains to the information at hand NONE
Error messages if any NONE
If not the entire code, then at least some of it NONE
Sample data (before and after sample worksheets, add as attachments here) NONEHey all. I have been tasked with a project to create a user form at work. I have created most of it and worked through some of the macros myslef and gotten help from my boss the macro guru as well. Unfortunately he has cut off his life line and told me to search the net for a solutiuon.

I have and found tons of stuff that is not exactly what I needed or I could not get it to work.

What I would like to do is either 1) set multiple fields to required on a user form with a msg box appearing stating "blah blah you need to fill this in" or 2) an on save event that checks specified form fields and lists what needs to be filled in/has missing data.

Any help would be greatly appreciated. I do have code in the form now, but it does not relate to this specific task, so I am not posting. I have tried many, many searches on this forum and google etc to no avail. It is possible that I am just completely useless, we'll see.

Let me know your thoughts and if I can give you more details. Thanks!

Mike

fumei
09-28-2010, 03:41 PM
Make a decision.

Is this for a userform, or the document?

If it is the userform then put error trapping, reuired logic in any event that is going to terminate the userform. In other words, do not let them close the userform until the data is correct. As for doing that, YOU have to test all the required fields.

In the document, it is a little trickier. You can trap the Save event, the BeforeClose event.

If you are dealing with formfields in the document, you can trap each formfield individually if you want.

MBGoodwin
09-29-2010, 07:16 AM
Thanks for the reply. I have seen those two options in my searches. It is a word document set up as a form with text boxes, and the like.

Preferably I would like to be able to make certain fields required. This form could be partially filled out and saved (while waiting on other info) by the user, so I do not really want to limit that ability (to save).

How can I make certain fields required?

Thanks

fumei
09-29-2010, 09:11 AM
You can not make them required.

You can test them to see if they have the values you decide they need.

You can try http://word.mvps.org/FAQs/TblsFldsFms/ValidateFFields.htm]here as a possible starting place.

MBGoodwin
09-29-2010, 09:57 AM
Gerry,

Thanks. If I understand it correctly, the sample macro on the site will allow me to specify what a value/format should be in a text box and if you tab past it will bring you back and give you a message box to fix.

It is saying in the example that the first three characters in the text box need to be "KLM". Can this be modified? One field I have in particular is going to be an individuals or company's name, so I cannot define the first characters. Can I set to something different? Instead of saying if the left three characters are not (<>) "KLM", instead saying if the left first character is "" then msg box?

Also, where it has FormFields("Text2"), here is where I would replace the Text2 with the text box name for my particular field?

I actually just tried the macro below, and got a run time error 5941, "the requested member of the collection does not exist". I replaced Text2 with "PMCompany" (the name of the text box I am working with) in the example below. I pasted it into Project->Modules->New Macros.


Sub ExitText2()
With ActiveDocument.FormFields("Text2")
If Len(.Result) > 0 And Left$(.Result, 3) <> "KLM" Then
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText2"
MsgBox "The first three letters must be 'KLM'"
End If
End With
End Sub

Sub GoBacktoText2()
ActiveDocument.Bookmarks("Text2").Range.Fields(1).Result.Select
End Sub


Thanks so much for your assistance. I think I am getting closer.

fumei
09-29-2010, 10:03 AM
"Can I set to something different? Instead of saying if the left three characters are not (<>) "KLM", instead saying if the left first character is "" then msg box?"

Yes.

"Also, where it has FormFields("Text2"), here is where I would replace the Text2 with the text box name for my particular field?"

Yes.

MBGoodwin
09-29-2010, 11:04 AM
I changed the macro to be what I want (think I did it right). I am still getting errors. Here is what I have:
Sub Required_Field()
'
' Required_Field Macro
'
'
With ActiveDocument.FormFields("PMCompany")
If Len(.Result) > 0 And Left$(.Result, 1) = "" Then
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText2"
MsgBox "This field must be filled in."
End If
End With
End Sub

Sub GoBacktoText2()
ActiveDocument.Bookmarks("PMCompany").Range.Fields(1).Result.Select
End Sub

Still getting the error "run time error 5941, the requested member of the collection does not exist".

It appears to not like the first line of code, with active documnet.....

Any advice? Thanks!

fumei
09-29-2010, 11:16 AM
If it states it does not exist, then most likely it does not exist. Are you SURE you have the name correct?

BTW:
If Len(.Result) > 0 And Left$(.Result, 1) = ""
How can .Result be longer than 0 AND have the first character = ""?

MBGoodwin
09-29-2010, 11:27 AM
I get your comment on the VBA, I have changd to = 0.

I double checked the properties on the field and even copied and pasted the name, still did not work. Is a form field the same as a text box? On the properties box, the title is PMCompay textbox. Could this be the issue?

Thanks

fumei
09-29-2010, 11:43 AM
"Is a form field the same as a text box?"

Ummmm, no...yes...no.

There is a text formfield, but technically those can not be called textboxes, although people do. This is why we commonly ask, what KIND of textbox.

So, walk me through things.

1. How did you get this thing in the document?
2. How do you get Properties?

The Properties dialog should show as : Text Form Field Options

So, hmmm, if you do NOT have a formfield, what are you doing to show its properties. AND, if it is not a formfield, how are you putting in an OnExit macro??????

MBGoodwin
09-29-2010, 11:58 AM
I appreciate the help and apologize for not knowing what I am doing :dunno

It is a word 2007 doc. The fields on the form are created from the developer tab on the ribbon using various items from the Controls section. I have different types, date picker, text etc.

The ones I am working on are basic fillin the blank, what I call, text boxes. I get to properties by either clicking on design mode then properties and the field, or right clicking inthe field and selecting properties.

Thanks

fumei
09-29-2010, 12:40 PM
I do not use 2007, so I am still uncertain as to whether you are using legacy objects, or not. But it sounds like they are NOT formfields, but ActiveX controls.

In which case that whole thing with the code from the MVP site is out the window. That is for formfields.

Tell me, getting properties, does the object have a default name like TextBox1? Is one of the properties listed something like TabKeyBehavior, or Locked, or Value?

If so, yup, they are ActiveX, and so using FormFields("PMCompany") will fail with "5941, the requested member of the collection does not exist".

It does not exist (as a FormField). These controls do not have an OnExit macro ability. They have other events though, although not an Exit event.

MBGoodwin
09-29-2010, 01:30 PM
Gerry,

That is correct. The properties box has fields like TabKeyBehavior, MousePointer, ScrollBars etc.

What types of events can I use? And for my knowledge, if I were to do some searching on this would I look for something like "Word ActiveX events"?

Thanks so much for your help on this. Makes me feel a little better that what I was tryuing to do is not possible rather than me just doing it completely wrong. I ahve some experience with macros in Excel, so I am not a complete novice. I was really starting to feel defeated!

Thanks

fumei
09-29-2010, 01:50 PM
When you right click the control, instead of click properties, click View Code. This opens the VBE, and the ThisDocument code module. The default event (Change, I think) will be inserted into the module. To see the other events available, click the wee down arrow on the top right of the code module.

The left dropdown should show something like TextBox1, and the right dropdown should show Change. Clicking the dropdown you will see things like:

Error
GotFocus
KeyDown
KeyPress

etc.

These are the procedures (events) of the control.