Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在IF语句中应用userform答案_Vba_If Statement_Outlook_Userform - Fatal编程技术网

Vba 在IF语句中应用userform答案

Vba 在IF语句中应用userform答案,vba,if-statement,outlook,userform,Vba,If Statement,Outlook,Userform,我试图实现一个IF语句,将不同的用户答案(如sTitle和sSurename)添加到HTML主体、主题等。代码总是转到第二个选项,没有错误 而且,当我有一封空邮件时,我会看到签名,但当我更新邮件正文中的任何内容时,签名就会消失 Sub template() Dim myItem As Outlook.MailItem Dim strContact As String Dim strHTML As String Dim sType As String D

我试图实现一个IF语句,将不同的用户答案(如sTitle和sSurename)添加到HTML主体、主题等。代码总是转到第二个选项,没有错误

而且,当我有一封空邮件时,我会看到签名,但当我更新邮件正文中的任何内容时,签名就会消失

Sub template()

    Dim myItem As Outlook.MailItem
    Dim strContact As String
    Dim strHTML As String

    Dim sType As String
    Dim sTitle As String
    Dim sName As String
    Dim sSurname As String
    Dim sExpirydate As String
    Dim sGender As String

    With UserForm1

        sType = .ComboBox1.Value
        sTitle = .ComboBox2.Value
        sName = .TextBox1.Value
        sSurname = .TextBox2.Value
        sExpirydate = .TextBox3.Value
        sGender = .ComboBox3.Value

    End With

    Set myItem = CreateItem(olMailItem)
    strHTML = myItem.HTMLBody

    With myItem
        .BodyFormat = olFormatHTML

        If sType = "New" Then
            .HTMLBody = "<HTML><BODY><b>Dear " & sTitle & " " & sSurname & "</b> 111 the message text here.  </BODY></HTML>"
            .Subject = "New case"
        Else
            .HTMLBody = "<HTML><BODY><b>Dear " & sTitle & " " & sSurname & "</b> 222 the message text here.  </BODY></HTML>"
            .Subject = "Old case"
        End If  

        .OriginatorDeliveryReportRequested = True
        .ReadReceiptRequested = True

    End With

    myItem.Display

End Sub

从您初始化的
ComboBox1
判断,我认为您在测试
If
语句中的值是否错误。您可能应该测试
“Form1”
,而不是测试
“New”

由于声明了一个名为
sSurname
的变量,但随后使用了
sSurename
,因此也出现了输入错误。(与
sExpirydate
存在类似的问题)强烈建议将
Option Explicit
作为每个代码模块的第一行,这迫使您声明所有变量。这样,像这样的输入错误会在编译时而不是在运行时被捕获

'...
sSurname = .TextBox2.Value
sExpirydate = .TextBox3.Value    
'...
'...
If sType = "Form1" Then
    .HTMLBody = "<HTML><BODY><b>Dear " & sTitle & " " & sSurname & "</b> 111 the message text here.  </BODY></HTML>"
    .Subject = "New case"
Else
    .HTMLBody = "<HTML><BODY><b>Dear " & sTitle & " " & sSurname & "</b> 222 the message text here.  </BODY></HTML>"
    .Subject = "Old case"
End If
”。。。
sSurname=.TextBox2.Value
六吡喃盐=.TextBox3.Value
'...
'...
如果sType=“Form1”,则
.HTMLBody=“亲爱的”&sTitle&&sSurname&“111将消息文本显示在此处。”
.Subject=“新案例”
其他的
.HTMLBody=“亲爱的”&sTitle&&sSurname&“222将消息文本显示在此处。”
.Subject=“旧案例”
如果结束

逐步浏览代码,直到到达
If
语句,然后告诉我们sType的值是多少。(P.S.[iif函数]与[if语句]不同,所以我更新了你的标签。)Oops-我一定没有保存我的标签编辑-现在已经保存了。我已经更新了以前的帖子,并添加了Userfrom代码。我已经更新了所有问题(并在第一篇帖子中完成),结果相同,所有链接都丢失了,按“确定”按钮后总是转到“其他”。@folkstorm-这是一个全新的问题-现在它将不起作用,因为您在运行
模板
子例程之前卸载表单,所以用户设置的所有值都丢失了。您是对的,我用UserForm1.Hide替换了Unload-Me,效果很好。现在我应该可以完成这个宏了。非常感谢。
'...
sSurname = .TextBox2.Value
sExpirydate = .TextBox3.Value    
'...
'...
If sType = "Form1" Then
    .HTMLBody = "<HTML><BODY><b>Dear " & sTitle & " " & sSurname & "</b> 111 the message text here.  </BODY></HTML>"
    .Subject = "New case"
Else
    .HTMLBody = "<HTML><BODY><b>Dear " & sTitle & " " & sSurname & "</b> 222 the message text here.  </BODY></HTML>"
    .Subject = "Old case"
End If