Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 从Userform下拉菜单中选择电子邮件模板-需要对象_Vba_Forms_Outlook - Fatal编程技术网

Vba 从Userform下拉菜单中选择电子邮件模板-需要对象

Vba 从Userform下拉菜单中选择电子邮件模板-需要对象,vba,forms,outlook,Vba,Forms,Outlook,我正在尝试创建一个Outlook用户表单,操作员可以通过下拉菜单选择电子邮件模板 使用示例,这是Outlook窗体的代码,它运行良好 Private Sub UserForm_Initialize() With ComboBox1 .AddItem "Test" .AddItem "Template 2" .AddItem "Template 3" .AddItem "Template 7" .AddItem

我正在尝试创建一个Outlook用户表单,操作员可以通过下拉菜单选择电子邮件模板

使用示例,这是Outlook窗体的代码,它运行良好

Private Sub UserForm_Initialize()
    With ComboBox1
        .AddItem "Test"
        .AddItem "Template 2"
        .AddItem "Template 3"
        .AddItem "Template 7"
        .AddItem "Template 5"
        .AddItem "Template 6"
    End With
End Sub

Private Sub btnOK_Click()
    lstNum = ComboBox1.ListIndex
    Unload Me
End Sub
这是我开始编写的代码,用于选择模板。当我使用下拉菜单选择“测试模板”时,我在这里收到一个错误“测试。选择”高亮显示一个对象是必需的

Public lstNum As Long

Public Sub ChooseTemplate()

    Dim oMail As Outlook.MailItem
    Dim oContact As Outlook.ContactItem

    Dim strTemplate As String
    UserForm1.Show

    Select Case lstNum
    Case -1
    '  -1 is what you want to use if nothing is selected
         strTemplate = "Test"
    Case 0
         strTemplate = "template-1"
    Case 1
        strTemplate = "template-2"
    Case 2
         strTemplate = "template-3"
    Case 3
         strTemplate = "template-4"
    Case 4
         strTemplate = "template-5"
    End Select

    Test.Select
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
        .To = cell.Value
        .Subject = "Test Facility"
        .HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>Hi " You recently confirmed you require continued use of the test facility
            "<p>Many thanks and kind regards</p></BODY>" & Signature
        .Sensitivity = 2
        .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing

    cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True

    wb.Close savechanges:=True

    End If

    Set oMail = Nothing

End Sub
Public lstNum尽可能长
公共子选择器模板()
将oMail设置为Outlook.MailItem
Dim oContact作为Outlook.ContactItem
将strTemplate设置为字符串
UserForm1.Show
选择Case lstNum
案例1
“-1是在未选择任何内容时要使用的值
strTemplate=“测试”
案例0
strTemplate=“模板-1”
案例1
strTemplate=“模板-2”
案例2
strTemplate=“模板-3”
案例3
strTemplate=“模板-4”
案例4
strTemplate=“模板-5”
结束选择
测试。选择
Set-OutMail=OutApp.CreateItem(0)
出错时继续下一步
发邮件
.To=单元格.Value
.Subject=“测试设施”
.HTMLBody=“Hi”您最近确认需要继续使用测试设备
“非常感谢和亲切的问候”“&签名
.灵敏度=2
.发送
以
错误转到0
发送邮件=无
清理:
设置应用程序=无
Application.ScreenUpdating=True
wb.Close savechanges:=真
如果结束
设置oMail=Nothing
端接头

要从模板生成邮件,请参见

在Outlook中运行此代码以查看如何使用所选内容

Public lstNum As Long

Public Sub ChooseTemplate()

    Dim outMail As Outlook.MailItem

    UserForm1.Show

    Select Case lstNum

    ' Following the listbox entries

    Case -1
    '  -1 is what you want to use if nothing is selected
        Set OutMail = CreateItemFromTemplate("Path\to\test.oft")

    Case 0
        Set OutMail = CreateItemFromTemplate("Path\to\test.oft")

    Case 1
        Set OutMail = CreateItemFromTemplate("Path\to\template-2.oft")

    Case 2
        Set OutMail = CreateItemFromTemplate("Path\to\template-3.oft")

    Case 3
        Set OutMail = CreateItemFromTemplate("Path\to\template-7.oft")

    Case 4
        Set OutMail = CreateItemFromTemplate("Path\to\template-5.oft")

    Case 5
        Set OutMail = CreateItemFromTemplate("Path\to\template-6.oft")

    End Select

    ' Use for a specific purpose not randomly
    ' On Error Resume Next

    With OutMail
        .To = "cell.Value"    ' For this Outlook demo

        ' This should be in the template
        ' .Subject = "Test Facility"
        ' .HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>Hi " You recently confirmed you require continued use of the test facility
        '       "<p>Many thanks and kind regards</p></BODY>" & Signature
        ' .Sensitivity = 2

        .Display
    End With

    ' On Error GoTo 0

    cleanup:
        Set OutMail = Nothing

  End Sub
Public lstNum尽可能长
公共子选择器模板()
将outMail设置为Outlook.MailItem
UserForm1.Show
选择Case lstNum
'在列表框条目之后
案例1
“-1是在未选择任何内容时要使用的值
Set-OutMail=CreateItemFromTemplate(“Path\to\test.oft”)
案例0
Set-OutMail=CreateItemFromTemplate(“Path\to\test.oft”)
案例1
Set-OutMail=CreateItemFromTemplate(“Path\to\template-2.oft”)
案例2
Set-OutMail=CreateItemFromTemplate(“Path\to\template-3.oft”)
案例3
Set-OutMail=CreateItemFromTemplate(“Path\to\template-7.oft”)
案例4
Set-OutMail=CreateItemFromTemplate(“Path\to\template-5.oft”)
案例5
Set-OutMail=CreateItemFromTemplate(“Path\to\template-6.oft”)
结束选择
“用于特定用途,而不是随机使用
'出现错误时,请继续下一步
发邮件
.To=“cell.Value””用于此Outlook演示
'这应该在模板中
'.Subject=“测试设施”
'.HTMLBody=“Hi”您最近确认需要继续使用测试设备
“非常感谢和亲切的问候

”&签名 “.灵敏度=2 .展示 以 '在出现错误时转到0 清理: 发送邮件=无 端接头
要从模板生成邮件,请参见

在Outlook中运行此代码以查看如何使用所选内容

Public lstNum As Long

Public Sub ChooseTemplate()

    Dim outMail As Outlook.MailItem

    UserForm1.Show

    Select Case lstNum

    ' Following the listbox entries

    Case -1
    '  -1 is what you want to use if nothing is selected
        Set OutMail = CreateItemFromTemplate("Path\to\test.oft")

    Case 0
        Set OutMail = CreateItemFromTemplate("Path\to\test.oft")

    Case 1
        Set OutMail = CreateItemFromTemplate("Path\to\template-2.oft")

    Case 2
        Set OutMail = CreateItemFromTemplate("Path\to\template-3.oft")

    Case 3
        Set OutMail = CreateItemFromTemplate("Path\to\template-7.oft")

    Case 4
        Set OutMail = CreateItemFromTemplate("Path\to\template-5.oft")

    Case 5
        Set OutMail = CreateItemFromTemplate("Path\to\template-6.oft")

    End Select

    ' Use for a specific purpose not randomly
    ' On Error Resume Next

    With OutMail
        .To = "cell.Value"    ' For this Outlook demo

        ' This should be in the template
        ' .Subject = "Test Facility"
        ' .HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>Hi " You recently confirmed you require continued use of the test facility
        '       "<p>Many thanks and kind regards</p></BODY>" & Signature
        ' .Sensitivity = 2

        .Display
    End With

    ' On Error GoTo 0

    cleanup:
        Set OutMail = Nothing

  End Sub
Public lstNum尽可能长
公共子选择器模板()
将outMail设置为Outlook.MailItem
UserForm1.Show
选择Case lstNum
'在列表框条目之后
案例1
“-1是在未选择任何内容时要使用的值
Set-OutMail=CreateItemFromTemplate(“Path\to\test.oft”)
案例0
Set-OutMail=CreateItemFromTemplate(“Path\to\test.oft”)
案例1
Set-OutMail=CreateItemFromTemplate(“Path\to\template-2.oft”)
案例2
Set-OutMail=CreateItemFromTemplate(“Path\to\template-3.oft”)
案例3
Set-OutMail=CreateItemFromTemplate(“Path\to\template-7.oft”)
案例4
Set-OutMail=CreateItemFromTemplate(“Path\to\template-5.oft”)
案例5
Set-OutMail=CreateItemFromTemplate(“Path\to\template-6.oft”)
结束选择
“用于特定用途,而不是随机使用
'出现错误时,请继续下一步
发邮件
.To=“cell.Value””用于此Outlook演示
'这应该在模板中
'.Subject=“测试设施”
'.HTMLBody=“Hi”您最近确认需要继续使用测试设备
“非常感谢和亲切的问候

”&签名 “.灵敏度=2 .展示 以 '在出现错误时转到0 清理: 发送邮件=无 端接头
这项测试应该是什么?它在哪里声明和初始化(您使用它的方式表明它是一个对象)Hi@avb,感谢您抽出时间与我联系。Test是我模板的名称,但使用下面niton的解决方案,我已经能够让它工作了。非常感谢并问候克里斯,这个
测试应该是什么?它在哪里声明和初始化(您使用它的方式表明它是一个对象)Hi@avb,感谢您抽出时间与我联系。Test是我模板的名称,但使用下面niton的解决方案,我已经能够让它工作了。非常感谢并问候ChrisHi@niton,非常感谢您的帮助,它非常有效。向ChrisHi@niton致以亲切的问候,非常感谢您的帮助,效果非常好。你好,克里斯