Vba 将Outlook用户表单值应用于电子邮件模板
我想在userform中选择特定值,并根据选择重新格式化电子邮件模板并替换文本,或者只打开一封空电子邮件并将密钥/文本发送到电子邮件标题、正文等(如名称、姓氏等值) 用户表单 执行按钮代码Vba 将Outlook用户表单值应用于电子邮件模板,vba,templates,userform,Vba,Templates,Userform,我想在userform中选择特定值,并根据选择重新格式化电子邮件模板并替换文本,或者只打开一封空电子邮件并将密钥/文本发送到电子邮件标题、正文等(如名称、姓氏等值) 用户表单 执行按钮代码 Private Sub CommandButton1_Click() If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or _ ComboBox1 = "" Or ComboBox2 = "" Or ComboBox3
Private Sub CommandButton1_Click()
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or _
ComboBox1 = "" Or ComboBox2 = "" Or ComboBox3 = "" Then
MsgBox ("Fill in all Boxes")
End If
End Sub
Sub template()
Dim myItem As Outlook.MailItem
Dim strContact As String
Dim strHTML As String
Dim typeofapplication As String
Dim title As String
Dim name As String
Dim surename As String
Dim expierydate As String
Dim gender As String
typeofapplication = ComboBox1.Value
title = ComBox2.Value
name = TextBox1_Change.Value
surename = TextBox2_Change.Value
expierydate = TextBox3_Change.Value
gender = ComBox3.Value
Set myItem = Application.CreateItemFromTemplate("C:\test.oft")
strHTML = myItem.HTMLBody
myItem.Display
End Sub
代码一直工作到“填写所有方框”。按下OK(确定)按钮后,它会在myItem.Display(显示)
上停止
如果我不使用userform,则打开模板是有效的。正如我在评论中所述,您应该在CommandButton1\u Click sub中调用template sub。当您的条件为真时,您也应该退出sub,因为您不希望在任何文本框或组合框为空时运行代码。应该是这样的:
Private Sub CommandButton1_Click()
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or ComboBox1 = "" Or ComboBox2 = "" Or ComboBox3 = "" Then
MsgBox ("Fill in all Boxes")
Exit Sub
End If
template
End Sub
Set myItem = Application.CreateItemFromTemplate("C:\test.oft")
strHTML = myItem.HTMLBody
Unload Me
myItem.Display
End Sub
其次,当出现问题时,您将收到一条错误消息,通常带有错误号和一些描述。我必须尝试你的代码来得到这个错误是什么
错误为:“Outlook无法执行此操作,因为有一个对话框处于打开状态。请关闭该对话框,然后重试。”如果您在问题中指出了这一点,您会得到一个即时回答,即“卸载我”。下次发布问题时,也应指出错误
无法使其运行,因为首先需要关闭userform才能打开模板电子邮件。因此,在您的代码中插入Unload Me
,如下所示:
Private Sub CommandButton1_Click()
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or ComboBox1 = "" Or ComboBox2 = "" Or ComboBox3 = "" Then
MsgBox ("Fill in all Boxes")
Exit Sub
End If
template
End Sub
Set myItem = Application.CreateItemFromTemplate("C:\test.oft")
strHTML = myItem.HTMLBody
Unload Me
myItem.Display
End Sub
如果您的代码应该在单击CommandButton1之后工作,那么您应该调用template sub。在end If之后调用template,在end If之前退出sub。我已尝试将sub template添加到CommandButton1\u Click。它在myItem.Display上停止是什么意思?它是否返回错误?你能说得更具体些吗?是的,当我调试它时,它指的是“myItem.Display”行,谢谢你的回复,很抱歉没有具体说明,我是一个新手。我会在早上的第一件事应用你的建议,因为在当前的pc上我没有outlook。