VBA - Using if statement within a string for outlook email from Excel

filarap

New Member
Joined
May 15, 2015
Messages
33
Hi all,

I am creating a user form within Excel file that is used to input data into the sheet, but also it is sending an email from outlook with specifics from that user form.

I have come to a standstill when trying to use option buttons from the user form.
I have tried to simply continue the text with:
if type1 = true then
"text text text"
else
"text2 text2 text2"
end if

However this clearly does not work. I am guessing i can do it if i use the cell from the sheet, but my goal is to get this done directly from the form. Below is the code i have so far:

Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String


On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Dear colleagues " & vbNewLine & vbNewLine & firstname & " " & surname & " called in sick with their first day of absence being " & _
startday & "." & vbNewLine & "Reason for the absence is " & reason & "(" & code & ")"
'This is where is need to continue if option buttons




On Error Resume Next
With xOutMail
.To = "filarap@yahoo.com"
.CC = ""
.BCC = ""
.Subject = "text of subject " & firstname & " " & surname
.Body = xMailBody
.send 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing


Regards
Filarap
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hello Filarap,

Are the Options Buttons on a worksheet or a VBA UserForm?

If the buttons are on a worksheet, what is the worksheet's name?

Do the buttons form a single functional group?
 
Upvote 0
Hi Ross,

Buttons are on the userform. They are creating one group (for example options for answer to a question are "Yes" and "No"). Names for the option buttons for the code above are type1 and type2.

Kind rgeards
Filarap
 
Upvote 0
Perhaps something like this.
Code:
Dim strReason As String

    ' other code

    If OptionButton1.Value = True Then
        strReason = " reason 1"
    ElseIf OptionButton2.Value = True Then
        strReason = " reason 2"
    End If

    xMailBody = "Dear colleagues " & vbNewLine & vbNewLine & firstname & " " & surname & " called in sick with their first day of absence being " & _
                startday & "." & vbNewLine & "Reason for the absence is " & strReason
 
Upvote 0
Hello filarap,

Here is code you can add to button on the UserForm to display the option button selected. Let me know if you need help adding this to your current macro.

Code:
Private Sub CommandButton1_Click()


    Dim Msg As String
    
        Msg = "(none)"
        
        If Type1.Value = True Then Msg = Type1.Caption
        If Type2.Value = True Then Msg = Type2.Caption
        
        MsgBox "You selected option " & Msg
        
End Sub
 
Upvote 0
Thank you very much Ross,

This works great :)

Can you please advise why did you use msg = "none part". I understand the rest of the code, but this part is confusing me?

Kind regards
Filarap
 
Upvote 0
Hello filarap,

This was added in case the user clicks the command button before selecting an option button. This can be used to easily validate the user has chosen an option before the userform is closed.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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