#1
|
|||
|
|||
Check Form fields populated before save
Hi All
I have a sample form with a number of form fields. What I want to be able to do is check that all form fields have been populated before the user is allowed to save the form using VBA. Any advice/help/guidance would be appreciated. A copy of my sample form is attached for reference. |
#2
|
||||
|
||||
The following code, when placed in the ‘ThisDocument’ module, prevents the document being closed if it contains any formfields that have not been filled in.
Code:
Private Sub Document_Close() Dim FFld As FormField, Doc As Document Set Doc = ActiveDocument For Each FFld In Doc.FormFields If Trim(FFld.Result) = "" Then MsgBox "Please complete all the items" If Doc.Saved = True Then Doc.Reload Exit Sub End If Next End Sub Code:
Private Sub Document_Close() Dim FFld As FormField, Doc As Document Set Doc = ActiveDocument For Each FFld In Doc.FormFields Select Case FFld.Name Case "Text1", "Text7" If Trim(FFld.Result) = "" Then MsgBox "Please complete the mandatory items" If Doc.Saved = True Then Doc.Reload Exit Sub End If End Select Next End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul
Many thanks for your prompt response. I will test it out tomorrow. Again - many thanks. Kind regards Tony |
#4
|
||||
|
||||
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Hi Paul
I have tested your first solution. The message box is displayed if I try and close the document without populating all fields. However when I press OK the routine falls over on Doc.Reload. Any ideas on a resolution please? Look forward to hearing from you. Kind regards Tony |
#6
|
||||
|
||||
Are you adding the macro to the document or it's template? I can't recall which I used when developing the code some years ago, but it may need to be added to the document's template, not to the document itself - which also means you can save the document in the docx format.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Hi Paul
Thanks for your response. I get the same issue. I am getting an Error '4198'. Ideally the code needs to be in the document as multiple people will be using this template locally on their machines. Thanks Tony |
#8
|
||||
|
||||
I don't have time to investgate fully right now, but a quick & dirty workaround would be to change:
If Doc.Saved = True Then Doc.Reload to: Doc.Saved = False: SendKeys "{ESC}"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Hi Paul
Many thanks for your response - that's seems to work exactly as I wanted it to. I will do some extensive testing now before I roll the solution out to the users. Thanks again. Greg - apologies for not responding to you sooner. Paul's solution was nearer to what I needed and appears to appears to work as I need it to. Thanks again. Kind regards Tony |
#10
|
|||
|
|||
Hi Paul
I now have the code working okay. However I need to link it to the "Save" or "Save As" commands. Would you advise how I can do this please. Once I have this I (or should I say "we") will have completed my little project :-). Thanks in advance. Tony |
#11
|
||||
|
||||
I'm not sure why you'd need to link it to either of those, since the document can't be exited anyway while the fields are incomplete. Disabling Save As, for example, means the user can't give the file a name before all fields are filled in.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
Hi Paul
Thanks for the feedback. My thinking was that a user may open the template and realise they don't have sufficient information to complete it and as a result would want to exit the template. However I will go with what I have at the moment and re-visit it should the need arise. Again many thanks for your help. Tony |
#13
|
||||
|
||||
Well, there's a 'gotcha' there - having created the new document, they can't exit if they realise they don't have sufficient information to complete it ...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to get the MS Word Form Fields Check box associated text and their value using VBA? | ramkumar.yoganathan | Word VBA | 1 | 11-25-2016 04:59 AM |
Using a VBA to show relevant sections in a form if first field is populated | chasidar | Word VBA | 0 | 12-01-2014 04:58 AM |
Add button or clickable text to form that opens up user's email client with To field populated | richiebabes | Word VBA | 3 | 09-04-2014 09:21 PM |
Enable "check spelling as you type" for Form Fields | zeroth | Word | 3 | 11-08-2012 08:57 AM |
Duplicating form fields (check-boxes) | Jānis | Word | 3 | 09-16-2012 12:09 PM |