Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-07-2017, 02:12 PM
OTPM OTPM is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
Check Form fields populated before save
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default 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.
Attached Files
File Type: docm Sample Form with Mandatory Fields.docm (38.8 KB, 11 views)
Reply With Quote
  #2  
Old 03-07-2017, 02:26 PM
macropod's Avatar
macropod macropod is offline Check Form fields populated before save Windows 7 64bit Check Form fields populated before save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
To test for whether designated formfields (in this case, “Text1” & “Text7”) have been completed, you might use code like:
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]
Reply With Quote
  #3  
Old 03-07-2017, 02:28 PM
OTPM OTPM is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
Check Form fields populated before save
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

Hi Paul
Many thanks for your prompt response.
I will test it out tomorrow.
Again - many thanks.
Kind regards
Tony
Reply With Quote
  #4  
Old 03-07-2017, 10:05 PM
gmayor's Avatar
gmayor gmayor is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

See also http://www.gmayor.com/formfieldmacros.htm
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 03-08-2017, 02:31 AM
OTPM OTPM is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
Check Form fields populated before save
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 03-08-2017, 04:52 AM
macropod's Avatar
macropod macropod is offline Check Form fields populated before save Windows 7 64bit Check Form fields populated before save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by OTPM View Post
when I press OK the routine falls over on Doc.Reload
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]
Reply With Quote
  #7  
Old 03-08-2017, 07:55 AM
OTPM OTPM is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
Check Form fields populated before save
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 03-08-2017, 04:09 PM
macropod's Avatar
macropod macropod is offline Check Form fields populated before save Windows 7 64bit Check Form fields populated before save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #9  
Old 03-09-2017, 01:26 AM
OTPM OTPM is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
Check Form fields populated before save
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 03-09-2017, 11:10 AM
OTPM OTPM is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
Check Form fields populated before save
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 03-09-2017, 05:01 PM
macropod's Avatar
macropod macropod is offline Check Form fields populated before save Windows 7 64bit Check Form fields populated before save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #12  
Old 03-10-2017, 07:04 AM
OTPM OTPM is offline Check Form fields populated before save Windows 10 Check Form fields populated before save Office 2016
Expert
Check Form fields populated before save
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 03-10-2017, 11:13 PM
macropod's Avatar
macropod macropod is offline Check Form fields populated before save Windows 7 64bit Check Form fields populated before save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check Form fields populated before save 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
Check Form fields populated before save 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
Check Form fields populated before save 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:24 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft