将 Outlook 用户表单值应用于电子邮件模板
Apply Outlook userform values to email template
我想 select 用户表单中的特定值并基于 selection 重新格式化电子邮件模板并替换文本或只打开一封空电子邮件并将 keys/text 发送到电子邮件标题, body 等(姓名等值)
用户表单。
Private Sub TextBox1_Change()
End Sub
Private Sub TextBox2_Change()
End Sub
Private Sub TextBox3_Change()
End Sub
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "Form1"
.AddItem "Form2"
End With
With ComboBox2
.AddItem "Mr"
.AddItem "Miss"
.AddItem "Mrs"
.AddItem "Ms"
End With
With ComboBox3
.AddItem "You"
.AddItem "He"
.AddItem "She"
End With
End 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")
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
代码工作到 "Fill in all Boxes"。按下 OK 按钮后,它停在 myItem.Display
.
如果我不使用用户表单,打开模板是有效的。
正如我在评论中所述,您应该在 CommandButton1_Click Sub 中调用模板 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
其次,当出现问题时,您会收到一条错误消息,通常带有错误编号和一些描述。我不得不尝试你的代码来了解这个错误是什么。
错误说:"Outlook can't do this because a dialog box is open. Please close it and try again." 如果您在问题中指出了这一点,您会得到一个即时答案 "Unload Me"。下次当你 post 一个问题时,你也应该指出错误。
您无法做到 运行,因为您首先需要关闭用户表单,才能打开模板电子邮件。所以在你的代码中插入 Unload Me
像这样:
Set myItem = Application.CreateItemFromTemplate("C:\test.oft")
strHTML = myItem.HTMLBody
Unload Me
myItem.Display
End Sub
我想 select 用户表单中的特定值并基于 selection 重新格式化电子邮件模板并替换文本或只打开一封空电子邮件并将 keys/text 发送到电子邮件标题, body 等(姓名等值)
用户表单。
Private Sub TextBox1_Change()
End Sub
Private Sub TextBox2_Change()
End Sub
Private Sub TextBox3_Change()
End Sub
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "Form1"
.AddItem "Form2"
End With
With ComboBox2
.AddItem "Mr"
.AddItem "Miss"
.AddItem "Mrs"
.AddItem "Ms"
End With
With ComboBox3
.AddItem "You"
.AddItem "He"
.AddItem "She"
End With
End 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")
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
代码工作到 "Fill in all Boxes"。按下 OK 按钮后,它停在 myItem.Display
.
如果我不使用用户表单,打开模板是有效的。
正如我在评论中所述,您应该在 CommandButton1_Click Sub 中调用模板 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
其次,当出现问题时,您会收到一条错误消息,通常带有错误编号和一些描述。我不得不尝试你的代码来了解这个错误是什么。
错误说:"Outlook can't do this because a dialog box is open. Please close it and try again." 如果您在问题中指出了这一点,您会得到一个即时答案 "Unload Me"。下次当你 post 一个问题时,你也应该指出错误。
您无法做到 运行,因为您首先需要关闭用户表单,才能打开模板电子邮件。所以在你的代码中插入 Unload Me
像这样:
Set myItem = Application.CreateItemFromTemplate("C:\test.oft")
strHTML = myItem.HTMLBody
Unload Me
myItem.Display
End Sub