取决于文本框值自动执行电子邮件文本

时间:2019-01-25 10:39:57

标签: excel vba

我正在尝试通过VBA发送一封包含用户表单信息的电子邮件,我可以使用下面的代码轻松地做到这一点,

我唯一遇到的问题是某些信息仅在表单中具有某个特定值时才需要,例如,除非另外指定,否则颜色将标准设置为“黑色”。

如果我在用户表单中将颜色设置为“黑色”,我不想在电子邮件中包含该颜色,那么我只希望在他们想要其他颜色(例如“白色”)时显示该颜色

如果文本框为“黑色”,是否有if语句跳过整行并继续发送电子邮件,并且仅在非黑色的情况下才将其包括在电子邮件中?

Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = csemail_txt.Value
    .CC = ""
    .BCC = ""
    .Subject = "Quote Reference: " & Quotenum_txt.Value
    .Body = "Hello" & csname_txt.Value & vbNewLine & _
    "Thank you for your enquiry, please find your quote below: " & vbNewLine & vbNewLine & _
     "Product Code: " & ProductCode_Combo.Value & vbNewLine & _
     "Dimensions: " & dimension_txt.Value & vbNewLine

    .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

较小的修正;-)

1 个答案:

答案 0 :(得分:0)

Option Explicit
Sub OutStr()
ColorStr = "white"

BodyStr = "Hello " & csname_txt.Value "!" & vbNewLine

If ColorStr = "black" Then
    'We don't talk about the color if the color is black ;-)
    'see https://www.google.com/search?q=henry+ford+color+black
    'BodyStr = "The color will be black as you know!"
Else
    BodyStr = BodyStr & "The color will be " & ColorStr & " as requested!" & vbNewLine
End If

BodyStr = BodyStr & "Your Board will have " & NrOfWheels & " wheels as requested!" & vbNewLine

if feature1 then BodyStr = BodyStr & "Let's talk about feature 1!" & vbNewLine
if feature2 then BodyStr = BodyStr & "Let's talk about feature 2!" & vbNewLine
if feature3 then BodyStr = BodyStr & "Let's talk about feature 3!" & vbNewLine

MsgBox BodyStr
End Sub