PDA

View Full Version : Solved: Can you enforce Mandatory Fields on exit?



Colin_Penman
03-31-2008, 05:12 AM
Hello.

In a Word Form, is it possible to prevent the Saving of a new document if all of the Mandatory Fields have not been completed?

I know how to create a pop up if a mandatory field has not been completed however this only applies when you cycle through the fields with the tab key. There is nothing to prevent me saving a document with a blank mandatory field.

I am using the following code for my Macro's:

Sub MustFillIn1()
If ActiveDocument.FormFields("Owner").Result = "" Then
Do
sInFld = InputBox("Please enter the Owner details.")
Loop While sInFld = ""
ActiveDocument.FormFields("Owner").Result = sInFld
End If
End Sub

Thanks

fumei
03-31-2008, 09:50 AM
You can override the SaveAs command. In other words, write your own SaveAs procedure.

Sub FileSaveAs()

' your error trapping

End Sub

Please use the VBA tags to post code.

Colin_Penman
04-01-2008, 06:28 AM
Hello, and thank you for your response.

I am relatively new to VBA and I understand that I can right my own code to perform an action to perform when SaveAs is clicked however I am not at all familiar with the actual code I would need to write.

Can you assist?

I currently have 9 fields that must be filled in as part of the document however there is no validation that ensures this is done when you save the document. The Macro's have been labelled MustFillIn1 - 9.

I have attached the document in question and the code for each Macro is as follows (I hope I have inserted this correctly this time!)


Sub MustFillIn1()
If ActiveDocument.FormFields("Owner").Result = "" Then
Do
sInFld = InputBox("Please enter the Owner details.")
Loop While sInFld = ""
ActiveDocument.FormFields("Owner").Result = sInFld
End If
End Sub


This is relatively urgent so your prompt response would be very gratefully received.

Thanks again.

fumei
04-01-2008, 02:01 PM
I am not even going to look at your file right now, as I react with a little dragging to anyone asking for my "prompt response". You are not paying me.

It is just as I wrote:

Sub FileSaveAs()

' your error trapping

End Sub


Or - again, without actually looking at your file:
Sub FileSaveAs()
Call MustFillin1
Call MustFillin2
Call MustFillin3
Call MustFillin4
Call MustFillin5
Call MustFillin6
Call MustFillin7
Call MustFillin8
Call MustFillin9
End Sub

Although I have no doubt that this would be not the most efficient way to do it. However, it IS:

Sub FileSaveAs()

' your error checking

End Sub

As I suggested.

"Macro's have been labelled MustFillIn1 - 9." Note: you do NOT have a MustFillin9.

Other comments.

1. Use Option Explicit. In the VBE, Tools > Options. On the Editor tab, check Require Variable Declaration. In the long run, this is a no-brainer. Start now.

2. OK, I did open the file, and I did put in your MustFillin procedures into FileSaveAs. It immediately gave me an error because - as you are not using Option Explicit - sInFld is not a valid variable, as it is not declared.

So I added it as a global variable, so it can be used in all your procedures.

It did prompt me for the formfields that are empty ( "" ), so I cheerfully entered:

Owner as "dgjaksghkah ", which is perfectly valid.

CurrentProblem as "sdhlh adljadlhj yadda"....which is perfectly valid.

RequiredModule as "whatever, I don't really care"....which is, of course, perfectly valid.

And so on.

3. You did not state what exactly you want to do with a File SaveAs, so I made it just show the dialog, as in:

Sub FileSaveAs()
Call MustFillIn1
Call MustFillIn2
Call MustFillIn3
Call MustFillIn4
Call MustFillIn5
Call MustFillIn6
Call MustFillIn7
Call MustFillIn8
Application.Dialogs(wdDialogFileSaveAs).Show
End Sub

4. Doing this as a document (.doc) file is not a good thing. It would be better to use a template (.dot).

Hopefully this is prompt enough for your relatively urgent need.

Colin_Penman
04-02-2008, 02:58 AM
Thanks for getting back to me so quickly, it really is appreciated.

Apologies if I sounded a little rude by asking for a prompt response, it wasn't intended. Am just under a little pressure to get this sorted is all, perhaps a little over my head!

One thing though, I can see you have attached a doc but I'm unable to view it....is there any way round this?

Thanks again.

Colin_Penman
04-02-2008, 02:59 AM
Please ignore last request, I can view it now. Thanks again.