Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-05-2016, 01:56 PM
derajlance derajlance is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2013
Novice
Disable email function of command button if all form fields are not completed
 
Join Date: May 2016
Posts: 18
derajlance is on a distinguished road
Default Disable email function of command button if all form fields are not completed

Now that I have my forms completed and emails sending the way I need them to send, I want to tweak a few things.



Is there any way to disable the email function of a command button if all form fields are not completed? If not, is there a way to prevent moving to the next field until the one before it is filled out?

I have figured out how to display an error upon exiting the form field with the following code:

Code:
Sub ExitText1()
    With ActiveDocument.FormFields("Text1")
        If Len(.Result) < 1 Then
            Application.OnTime When:=Now + TimeValue("00:00:001"), Name:="GoBacktoText1"
            MsgBox "This is a required field."
        End If
    End With
End Sub

But this does not prevent the user from submitting the form and sending the email.

Thanks in advance!
Reply With Quote
  #2  
Old 05-05-2016, 10:05 PM
gmayor's Avatar
gmayor gmayor is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

There is more on validating form fields at http://www.gmayor.com/formfieldmacros.htm however you cannot force users to run the macros.

As you are using macros anyway then it might make more sense to provide a userform to collect the data, write it to the document and to submit the form by e-mail. That way you can more easily check whether the required fields have been filled.

The alternative is to add code to the submit button to validate each of the required fields before allowing the process to complete.
__________________
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
  #3  
Old 05-06-2016, 08:15 AM
derajlance derajlance is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2013
Novice
Disable email function of command button if all form fields are not completed
 
Join Date: May 2016
Posts: 18
derajlance is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
The alternative is to add code to the submit button to validate each of the required fields before allowing the process to complete.
I think this is what I am trying to accomplish. My goal is that when they click "submit", it validates the form fields and if any of them are blank, the submit process will not complete and will display a message stating, "Field _ cannot be blank. Please complete the form and try again."

I have found several solutions to giving pop-up reminders, but nothing to terminate the process when the reminder shows.
Reply With Quote
  #4  
Old 05-06-2016, 09:15 AM
derajlance derajlance is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2013
Novice
Disable email function of command button if all form fields are not completed
 
Join Date: May 2016
Posts: 18
derajlance is on a distinguished road
Default

I think another option would be something like this:

Code:
    With ActiveDocument.FormFields("TextBox1")
        If Len(.Result) < 1 Then CommandButton1.Enabled = False
Not sure if that would work and if so, where it would need to go.
Reply With Quote
  #5  
Old 05-06-2016, 09:04 PM
gmayor's Avatar
gmayor gmayor is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

Presumably this relates to your other thread, in which case, immediately after the DIM statements at the top of the macro, enter or copy the following.
Code:
Dim oFF As FormField

    For Each oFF In ActiveDocument.FormFields
        If oFF.Result = "" Then
            oFF.Select
            MsgBox "Field cannot be left empty"
            GoTo lbl_Exit
        End If
    Next oFF
__________________
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
  #6  
Old 05-08-2016, 08:44 AM
derajlance derajlance is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2013
Novice
Disable email function of command button if all form fields are not completed
 
Join Date: May 2016
Posts: 18
derajlance is on a distinguished road
Default

Thanks again Graham. I will test this tomorrow and will update the thread. I truly appreciate your assistance!
Reply With Quote
  #7  
Old 05-09-2016, 10:08 AM
derajlance derajlance is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2013
Novice
Disable email function of command button if all form fields are not completed
 
Join Date: May 2016
Posts: 18
derajlance is on a distinguished road
Default

That worked!

One last question, simply because I thought I had found the solution on your website, but it doesn't seem to be working.

To prevent them from sending duplicates, I am trying to clear the text fields once the email from the command button is sent. I found your code for resetting form fields, but it doesn't seem to be resetting them.

I also tried to just add the following after the email code, but nothing happens:
Code:
Text1 = ""
Text 2 = ""
Reply With Quote
  #8  
Old 05-09-2016, 08:30 PM
gmayor's Avatar
gmayor gmayor is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

The following will reset the fields
Code:
Dim oFF As FormField
    For Each oFF In ActiveDocument.FormFields
        If oFF.Type = wdFieldFormTextInput Then
            oFF.TextInput.Clear
        End If
    Next oFF
    Set oFF = Nothing
You don't need the DIM statement if you are adding it to the end of the original macro as it is already defined.
__________________
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
  #9  
Old 05-10-2016, 02:20 PM
derajlance derajlance is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2013
Novice
Disable email function of command button if all form fields are not completed
 
Join Date: May 2016
Posts: 18
derajlance is on a distinguished road
Default

Alright, I have my userform done and it is sending properly, but the code above to clear afterwards isn't working. It doesn't seem to be doing anything at all.

I am not sure if it is because I have the application set to not visible or what.
I am going to post my full code below in hopes that it may help someone to help me identify the issue. I want to either have the form clear and ready to be filled out and submitted again after the email is sent OR I want to completely exit the document so they have to open it again and start from scratch.

This document:
Code:
Sub AutoNew()
Application.Visible = False
UserForm1.Show
End Sub
UserForm1:
Code:
Private Sub UserForm1_Initialize()
Me.Caption = "Escalation"
Me.Label1.Caption = "SVM #:"
Me.Label2.Caption = "Claim #:"
Me.Label3.Caption = "Customer Name:"
Me.Label4.Caption = "Provider # and/or name:"
Me.Label5.Caption = "Reason for escalation:"
Me.CommandButton1.Caption = "Submit"
End Sub


Private Sub UserForm_Terminate()
Application.Quit SaveChanges:=False
End Sub

Private Sub CommandButton1_Click()

    Set SVM = ActiveDocument.Bookmarks("SVM").Range
    SVM.Text = Me.TextBox1.Value
    
    Set Claim = ActiveDocument.Bookmarks("Claim").Range
    Claim.Text = Me.TextBox2.Value
    
    Set Customer = ActiveDocument.Bookmarks("Customer").Range
    Customer.Text = Me.TextBox3.Value
    
    Set Provider = ActiveDocument.Bookmarks("Provider").Range
    Provider.Text = Me.TextBox4.Value
    
    Set Reason = ActiveDocument.Bookmarks("Reason").Range
    Reason.Text = Me.TextBox5.Value
    

Static running As Boolean
If running Then Exit Sub
If TextBox1 = "" Or TextBox2 = "" Or TextBox3 = "" Or TextBox4 = "" Or TextBox5 = "" Then MsgBox "All fields are required. Complete the form before clicking submit.": Exit Sub
running = True

Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objInspector As Object
Dim objDoc As Word.Document
Dim objRange As Range

    ActiveDocument.Content.Copy
    On Error Resume Next
    Set objOutlook = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        MsgBox "Outlook is not running."
        GoTo lbl_Exit
    End If
    On Error GoTo 0

    Set objOutlookMsg = objOutlook.CreateItem(0)
    With objOutlookMsg
        .to = "email"
        .cc = "email"
        .Subject = "Escalation"
        Set objInspector = .GetInspector
        Set objDoc = objInspector.WordEditor
        Set objRange = objDoc.Range(0, 0)
        .Display
        objRange.Paste
        .send
    End With
lbl_Exit:
    Set objDoc = Nothing
    Set objRange = Nothing
    Set objOutlookMsg = Nothing
    Set objInspector = Nothing
    Set objOutlook = Nothing
    Exit Sub
End Sub
Reply With Quote
  #10  
Old 05-10-2016, 09:06 PM
gmayor's Avatar
gmayor gmayor is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

Please see the attached example, which you can modify as required. You can run the macro as many times as you wish and the document will be updated to reflect the form contents. The form is unloaded each time so does not retain the values. Create a new document from the template.
Attached Files
File Type: dotm Example.dotm (29.5 KB, 14 views)
__________________
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
  #11  
Old 05-11-2016, 01:05 PM
derajlance derajlance is offline Disable email function of command button if all form fields are not completed Windows 10 Disable email function of command button if all form fields are not completed Office 2013
Novice
Disable email function of command button if all form fields are not completed
 
Join Date: May 2016
Posts: 18
derajlance is on a distinguished road
Default

It took a while, but finally got this taken care of. Thank you again!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable email function of command button if all form fields are not completed Email from Command Button derajlance Word VBA 6 05-05-2016 07:40 AM
Submit to Email Command Button rob7676 Word VBA 0 08-20-2015 05:05 AM
Disable email function of command button if all form fields are not completed command button that generates a popup for sending email oduntan Word VBA 4 03-21-2013 02:15 PM
Disable email function of command button if all form fields are not completed Command button - save in temp folder and send email with attachment bigbird69 Word VBA 13 11-18-2012 10:06 PM
Disable email function of command button if all form fields are not completed Word VBA: How to create a command button and trigger a function? tinfanide Word VBA 2 12-02-2011 05:51 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:35 AM.


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