Search code examples
vbams-wordcomboboxuserform

UserForm Include TextBox and/or ComboBox Values in strbody if certain conditions are met


Below you will see several TextBox and ComboBox Values.

In another section of the code ComboBox6 has items "1", "2", and "3".

If ComboBox 6 Value is "1" I want string to Skip to not include this rows: & " [" & ComboBox6.Value & "] " & TextBox9.Value _ & " [" & ComboBox7.Value & "] " & TextBox10.Value _

If ComboBox 6 Value is "2" I want the string not include this row: & " [" & ComboBox7.Value & "] " & TextBox10.Value _

If ComboBox 6 Value is "3" then the code can include everything.

Any help is greatly appreciated.

*** I am a complete novice. ***

I have the code below, but I don't know how to modify it to do what I need.

Private Sub CommandButton1_Click()
   ' Unload Me '(Closes your form and removes everything associated with it from memory)
    
    ' Working in Office 2000-2016
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
'    Set OutMail = Application.ActiveExplorer.Selection.Item(1)
    Set OutMail = OutApp.CreateItem(0)

    strbody = "<BODY style=font-size:13pt;font-family:Arial>" _
              & "<b><u>" & Label1 & "</u></b>" _
              & "<br><br>" & Label5 & "<span style='background-color:rgb(192, 192, 192)'>" & TextBox3.Value & "</span>" _
              & "<br><br>" & Label6 & "<span style='background-color:rgb(192, 192, 192)'>" & TextBox4.Value & "</span>" _
              & "<br><br>" & Label7 & "<span style='background-color:rgb(192, 192, 192)'>" & ComboBox3.Value & "</span>" _
              & "<br><br>" & Label8 & "<span style='background-color:rgb(192, 192, 192)'>" & TextBox5.Value & "</span>" _
              & "<br><br>" & Label9 & "<span style='background-color:rgb(192, 192, 192)'>" & TextBox6.Value & "</span>" _
              & "<br><br>" & Label10 & "<span style='background-color:rgb(192, 192, 192)'>" & TextBox7.Value & "</span>" _
              & "<br><br>" & Label11 & "<span style='background-color:rgb(192, 192, 192)'>" & TextBox8.Value & "</span>" _
              & "<br><br>" & Label12 & "<span style='background-color:rgb(192, 192, 192)'>" & ComboBox4.Value & "</span>" _
              & "<br><br>" & Label13 & "<span style='background-color:rgb(192, 192, 192)'>" _ 
              & " [" & ComboBox6.Value & "] " & TextBox9.Value _
              & " [" & ComboBox7.Value & "] " & TextBox10.Value _
              & " [" & ComboBox8.Value & "] " & TextBox11.Value & "</span>" _
              & "<br><br>" & Label14 & "<span style='background-color:rgb(192, 192, 192)'>" & ComboBox9.Value & TextBox12.Value & "</span>" _
              & "<br><br>" & Label16 & "<span style='background-color:rgb(192, 192, 192)'>" & TextBox13.Value & "</span>"

    On Error Resume Next

    With OutMail
        .Display
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Case: " & TextBox3.Value & "; " & Label1
        .HTMLBody = strbody & "<br>" & .HTMLBody
        .Display
'        .Send
    End With

    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub

Solution

  • Untested, but something like this would be shorter:

    Private Sub CommandButton1_Click()
    
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String, cbVal As Long, str As String
        
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        cbVal = CLng(Me.ComboBox5.Value) 'convert to number
        
        'add items according to value (assumes can only be 1,2 or 3)
        str = " [" & ComboBox6.Value & "] " & TextBox9.Value
        If cbVal > 1 Then str = str & " [" & ComboBox7.Value & "] " & TextBox10.Value
        If cbVal > 2 Then str = str & " [" & ComboBox8.Value & "] " & TextBox11.Value
        
        strbody = "<BODY style=font-size:13pt;font-family:Arial>" _
                  & "<b><u>" & Label1 & "</u></b>" _
                  & "<br><br>" & Label5 & GetSpan(TextBox3.Value) _
                  & "<br><br>" & Label6 & GetSpan(TextBox4.Valu) _
                  & "<br><br>" & Label7 & GetSpan(ComboBox3.Value) _
                  & "<br><br>" & Label8 & GetSpan(TextBox5.Value) _
                  & "<br><br>" & Label9 & GetSpan(TextBox6.Value) _
                  & "<br><br>" & Label10 & GetSpan(TextBox7.Value) _
                  & "<br><br>" & Label11 & GetSpan(TextBox8.Value) _
                  & "<br><br>" & Label12 & GetSpan(ComboBox4.Value) _
                  & "<br><br>" & Label13 & GetSpan(str) _
                  & "<br><br>" & Label14 & GetSpan(ComboBox9.Value & TextBox12.Value) _
                  & "<br><br>" & Label16 & GetSpan(TextBox13.Value)
        
        
        On Error Resume Next
        
        With OutMail
            .Display
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = "Case: " & TextBox3.Value & "; " & Label1
            .HTMLBody = strbody & "<br>" & .HTMLBody
            .Display
        End With
        
        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    
    'return a string wrapping `v` in <span></span> with some styling
    Function GetSpan(v) As String
        GetSpan = "<span style='background-color:rgb(192, 192, 192)'>" & v & "</span>"
    End Function