userform to send email dependant on option button value

Newport Count

Active Member
Joined
Oct 25, 2009
Messages
328
Hi,

I have built a userform that sends two emails: one email to a distribution list that details some of the entries made in the form, and another email to a specific person if a customer name has been entered that does not match an existing name from the drop-down field. This works at the moment, but I want to make the emails relevant to the outcome of an radio button on the form.

I have cobbled together the email code from various sources (Ron de Bruin and help from various sites/forums) so it may not make much sense - but it works! Therefore, I'm not confident or competent enough to adapt it to what I want it to now do.


I have added two radio buttons that answer the question (label) "Has the adjustment billed?" - Yes or No radio buttons...Yes = "optBilledYes"; No = "optBilledNo".

- I need the 1st email to be sent (details of the invoice adjustment) only if the value chosen in the radio button is 'Yes'...so If "optBilledYes"
- I need 2nd email to state if a new customer has been added to be sent regardless of the value chosen in the option button.


code is present in: Private Sub cmdSaveData_Click()

Code:
Dim OutApp As Object
    Dim OutMail As Object
    Dim strBody As String
    Dim strSignature As String
    Dim strRecipients As String
    Dim ws As Worksheet
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    Set ws = Worksheets("DropDowns")
    

    With OutMail
        .Display
    End With
    
    
        strRecipients = ws.Range("EmailRecipients")

        strSignature = OutMail.HTMLBody
            
        strBody = "< p style='font-family:calibri;font-size:19' >"
            strBody = strBody & "Hello,"
            strBody = strBody & "<br><br>I have just logged an Invoice Adjustment:"
            strBody = strBody & "<br><br>Customer Name<br>" & cboCustomerName.Value
            strBody = strBody & "<br><br>Invoice:<br>" & txtInvoicenumber.Value
            strBody = strBody & "<br><br><br>Many thanks," & strSignature & "< /p>"
            

    With OutMail
        .To = strRecipients
        .CC = ""
        .BCC = ""
        .Subject = "New Invoice Adjustment - " & cboCustomerName.Value & " - " & txtInvoicenumber.Value
        .HTMLBody = strBody
        .Send
    End With
    

    Set OutMail = Nothing
    Set OutApp = Nothing
    
    
    
    If IsError(Application.Match(cboCustomerName.Value, ws.Range("CustomerName"), 0)) Then

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
            
    With OutMail
        .To = "myname@email.com"
        .Subject = "New Customer added to the Adjustment Tracker - " & cboCustomerName.Value
        .Send
    End With
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    End If

PS: I have added an extra space to the < p> & < /p> coding to display it correctly above!


I have tried many different options (mainly using If optBilledYes = True then...) but I receive debug errors which I don't know how to resolve. relating to the "If IsError" part of the 2nd email code - any help would be much appreciated!


Hopefully a simple fix (as usual!).


many thanks,
Rich
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Well I found a solution myself - I amended the above code as follows:

Code:
Dim OutApp As Object
    Dim OutMail As Object

    Dim ws As Worksheet
    
    Set ws = Worksheets("DropDowns")
    
    If optBilledYes = True Then
    
    Dim strBody As String
    Dim strSignature As String
    Dim strRecipients As String
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    Set ws = Worksheets("DropDowns")
    

    With OutMail
        .Display
    End With

    
        strRecipients = ws.Range("EmailRecipients")

        strSignature = OutMail.HTMLBody
            
        strBody = "< p style='font-family:calibri;font-size:19' >"
            strBody = strBody & "Hello,"
            strBody = strBody & "I have just logged an Invoice Adjustment:"
            strBody = strBody & "Customer Name" & cboCustomerName.Value
            strBody = strBody & "Invoice:" & txtInvoicenumber.Value
            strBody = strBody & "Many thanks," & strSignature & "< /p>"
            

    With OutMail
        .To = strRecipients
        .CC = ""
        .BCC = ""
        .Subject = "New Invoice Adjustment - " & cboCustomerName.Value & " - " & txtInvoicenumber.Value
        .HTMLBody = strBody
        .Send
    End With
    

    Set OutMail = Nothing
    Set OutApp = Nothing
    
    
    
    If IsError(Application.Match(cboCustomerName.Value, ws.Range("CustomerName"), 0)) Then

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
            
    With OutMail
        .To = "myname@email.com"
        .Subject = "New Customer added to the Adjustment Tracker - " & cboCustomerName.Value
        .Send
    End With
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    End If


I think my issue was with the Dim combinations - I needed to separate the shared and individual features for each email. Quite a basic solution really - I knew there was one!

Thanks to anyone that looked.


Cheers,
Rich
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top