Consulting

Results 1 to 4 of 4

Thread: DocumentBeforeClose Help

  1. #1
    VBAX Newbie
    Joined
    Nov 2021
    Posts
    1
    Location

    DocumentBeforeClose Help

    Hi all,

    I am trying to validate all form fields in a Word doc prior to close and message the user that these fields must be completed before the doc closes. Once they have clicked 'OK', I want the doc to stay open to allow them to update mandatory fields. This what I have so far (which doesn't work):

    Private Sub DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
     Dim intResponse As Integer
     Dim FFld As FormField
     Set Doc = ActiveDocument
     For Each FFld In Doc.FormFields
        If Trim(FFld.Result) = "" Then
        intResponse = MsgBox("Please enter a value in each field", vbOKOnly)
     'Cancel = True will prevent the document from closing once the sub is complete
     'Cancel = False will close the document after sub is complete.
     Cancel = True
     End If
     Next
    End Sub


    Thanks for your help!
    Last edited by Paul_Hossler; 11-19-2021 at 07:59 AM.

  2. #2
    There is no VBA documentbeforeclose event, so it is not surprising that it doesn't work
    If you are going to use macros then you could validate the fields as you complete the form - http://www.gmayor.com/formfieldmacros.htm but you cannot force users to run the macros.
    Personally I would not use legacy form fields. I would use content controls, which have their own exit events - but you still need macros.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Quote Originally Posted by Mrbrtsde View Post
    Hi all,

    I am trying to validate all form fields in a Word doc prior to close and message the user that these fields must be completed before the doc closes. Once they have clicked 'OK', I want the doc to stay open to allow them to update mandatory fields. This what I have so far (which doesn't work):

    Private Sub DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
     Dim intResponse As Integer
     Dim FFld As FormField
     Set Doc = ActiveDocument
     For Each FFld In Doc.FormFields
        If Trim(FFld.Result) = "" Then
        intResponse = MsgBox("Please enter a value in each field", vbOKOnly)
     'Cancel = True will prevent the document from closing once the sub is complete
     'Cancel = False will close the document after sub is complete.
     Cancel = True
     End If
     Next
    End Sub


    Thanks for your help!

    That is an Application event so you first have to create the class.

    In a new class module (named clsNormal) paste:


    Option Explicit
    Private WithEvents mWordApp As Word.Application
    Private Sub Class_Initialize()
      Set mWordApp = Word.Application
    End Sub
    
    Private Sub mWordApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    
    End Sub
    In a new standard module paste:

    Option Explicit
    Private oCls As clsNormal
    Sub AutoExec()
      Set oCls = New clsNormal
    lbl_Exit:
      Exit Sub
    End Sub
    Last edited by Aussiebear; 12-15-2021 at 04:48 AM. Reason: added code tags to submitted code
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Newbie
    Joined
    Feb 2024
    Posts
    2
    Location
    Private Sub DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    Dim intResponse As Integer
    Dim FFld As FormField
    Dim allFieldsCompleted As Boolean


    allFieldsCompleted = True


    For Each FFld In Doc.FormFields
    If Trim(FFld.Result) = "" Then
    intResponse = MsgBox("Please enter a value in each field", vbOKOnly)
    allFieldsCompleted = False
    Exit For ' Exits the loop immediately after finding an incomplete field
    EndIf
    next


    If Not allFieldsCompleted Then
    Cancel = True ' Prevents the document from closing when there are incomplete fields
    EndIf
    End Sub

    With this change, the document will not close if any fields are not completed. The user will receive a warning and can continue to update required fields before closing the document.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •