1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

How do i check that required Fields are filled OnPrint?

Discussion in 'Business Applications' started by FormPolice, Feb 20, 2019.

Thread Status:
Not open for further replies.
Advertisement
  1. FormPolice

    FormPolice Thread Starter

    Joined:
    Feb 20, 2019
    Messages:
    2
    How do i check that required Fields are filled OnPrint & OnSave??

    This Code used to work in older versions of Word, but it is not working now. I am using Office Professional Plus 2016. This is the code I used to use. Any Help would be greatly appreciated.

    Private Sub wordApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    Dim fld As FormField
    Dim ReqFields As New Collection

    ReqFields.Add Doc.FormFields("VehiclePlate")
    ReqFields.Add Doc.FormFields("VehicleColor")

    For Each fld In ReqFields
    If Trim(fld.Result) = "" Then
    MsgBox "You must fill in the " & fld.Range.Bookmarks(1).Name & " field.", vbOKOnly + vbExclamation, "Error"
    Cancel = True
    fld.Select
    Exit Sub
    End If
    Next fld
    End Sub
     
  2. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    I haven't done much Word VBA, but a couple of things come to mind.
    Has it ever worked in Word 2016?
    You haven't said what does happen.
    You do not have any Error Trapping.
    Have you reset the VBA Editor's Reference Library?
    I would add a line of code after "For Each fld In ReqFields"

    msgbox "Field - " & fld.Range.Bookmarks(1).Name & " = " & fld.Result

    To test whether or not the code differentiates between a field with & without data.
     
  3. FormPolice

    FormPolice Thread Starter

    Joined:
    Feb 20, 2019
    Messages:
    2
    sorry i guess i did leave a few things out.

    No it has not ever worked in Word 2016. I have an old form that i have used for a long time and now i am designing a new form and wanted to just copy the same validation code from the other form, but it doesn't work on the new form. It doesn't do anything that i can see. I print the form and save the form the same as always even though that code is in the form module.
     
  4. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    Have you got the correct Library References set?
    You should add Error Trapping to ensure that the code is not being stopped due to an error.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1223365

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice