首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从用户表单下拉菜单中选择电子邮件模板-对象是必需的

从用户表单下拉菜单中选择电子邮件模板-对象是必需的
EN

Stack Overflow用户
提问于 2017-07-26 17:04:56
回答 1查看 613关注 0票数 1

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

this为例,这是适用于Outlook的代码。

代码语言:javascript
复制
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

这是我开始拼凑的代码,用来选择模板。当我使用下拉菜单选择"Test Template“时,我在这里收到一个错误"Test.Select”,突出显示一个对象是必需的。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-29 20:32:35

要从模板生成邮件,请参见https://msdn.microsoft.com/VBA/Outlook-VBA/articles/application-createitemfromtemplate-method-outlook

代码语言:javascript
复制
Set MyItem = Application.CreateItemFromTemplate("C:\statusrep.oft")

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45322263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档