我有一些代码,可以使用不同的变量(即姓名、ID 号等)填充现有的 Outlook 电子邮件模板(带图形!)。它适用于存储在 OneDrive 上的“.oft”和“.msg”模板。当我可以在最后使用“mailItem.Send”而不是“mailItem.Display”时,这真是太棒了。这......只有当我信任代码时我才能做到。
最近我尝试更改代码,以便它根据变量调用 2 个模板中的 1 个。但在我调用模板后,它经常在“mailItem.Display”上给我错误 91(对象变量或未设置块变量),这让我发疯。
请注意,我宁愿做两个“If - Then”语句,而不是一个“If - Then - Else”语句,因为有 3 种变量类型,只有 2 种需要生成电子邮件。
它对于一种变量类型效果很好:
Do While Sheet1.Cells(r, 7) <> ""
'Call Outlook template
Set mailApp = CreateObject("Outlook.Application")
Set mailItem = mailApp.CreateItemFromTemplate("filepath\template.msg")
mailItem.Display
当我添加下面的 If 语句时,“mail.Item.Display”上出现 91 错误:
Do While Sheet1.Cells(r, 7) <> ""
Set mailApp = CreateObject("Outlook.Application")
If Sheet1.Cells(r, 4) = "Type A" Then Set mailItem = mailApp.CreateItemFromTemplate("filepath\Atemplate.msg")
If Sheet1.Cells(r, 4) = "Tyep B" Then Set mailItem = mailApp.CreateItemFromTemplate("filepath\Btemplate.msg")
mailItem.Display <<<<<ERROR
在代码的后面,If 语句对于不同的“.Subjects”效果很好。
With mailItem
.Display
If Sheet1.Cells(r, 4) = "Type A" Or Sheet1.Cells(r, 4) = "Type B" Then .To = Toemail
If Sheet1.Cells(r, 4) = "Type A" Then .Subject = "Login Information for " + Firstname + " " + Lastname + "
If Sheet1.Cells(r, 4) = "Type B" Then .Subject = "Your other Information is ready - " + Firstname + " " + Lastname
我尝试过的 我只尝试了一个 If 语句(并确保类型 A 是 Excel 中的第一行)。没有变化。
我尝试将 EmpID = Sheet1.Cells... 代码块移至 Set mailApp 上方...没有变化。
我尝试在两个 If 语句中从“Set mailItem”中删除“Set”。没有变化。
我尝试将 mailItem.Display 移至每个 If 语句的末尾,但错误移至我的变量(EmpID ...)
Do While Sheet1.Cells(r, 7) <> ""
Set mailApp = CreateObject("Outlook.Application")
If Sheet1.Cells(r, 4) = "Type A" Then Set mailItem = mailApp.CreateItemFromTemplate("filepath\Atemplate.msg") And mailItem.Display
If Sheet1.Cells(r, 4) = "Type B" Then Set mailItem = mailApp.CreateItemFromTemplate("filepath\Btemplate.msg") And mailItem.Display
EmpID = Sheet1.Cells(r, 1) <<<<<<ERROR
Lastname = Sheet1.Cells(r, 2)
Firstname = Sheet1.Cells(r, 3)
UserID = Sheet1.Cells(r, 5)
EmailID = Sheet1.Cells(r, 6)
Toemail = Sheet1.Cells(r, 7)
我尝试将 Sheet1.Cells (r, 4) 设置为字符串变量
Dim VariableType = String VariableType = Sheet1.Cells(r, 4)
但这也没有帮助。
不知道它是否相关,但我永远无法让它对一个变量进行 r+1 。尝试过:
VariableType = Sheet1.Cells(r, 4) If VariableType = "Type C" Then r = r + 1
和
If Sheet1.Cells(r, 4) = "Type C" Then r = r + 1
两者都不起作用。
完整代码如下:
Sub Send_email_from_template_my_code_1()
Dim EmpID As String
Dim Lastname As String
Dim Firstname As String
Dim VariableType As String
Dim UserID As String
Dim EmailID As String
Dim Toemail As String
Dim FromEmail As String
Dim mailApp As Object
Dim mailItem As Object
R=row number, and r = r + 1 will change the row number.
Dim r As Long
r = 2
Dim olInsp As Object
Dim wdDoc As Object
Dim oRng As Object
VariableType = Sheet1.Cells(r, 4)
If VariableType = "Type C" Then r = r + 1
Do While Sheet1.Cells(r, 7) <> ""
'Call Outlook template
Set mailApp = CreateObject("Outlook.Application")
Set mailItem = mailApp.CreateItemFromTemplate("filepath\template.msg")
mailItem.Display
EmpID = Sheet1.Cells(r, 1)
Lastname = Sheet1.Cells(r, 2)
Firstname = Sheet1.Cells(r, 3)
UserID = Sheet1.Cells(r, 5)
EmailID = Sheet1.Cells(r, 6)
Toemail = Sheet1.Cells(r, 7)
With mailItem
.Display
If Sheet1.Cells(r, 4) = "Type A" Or Sheet1.Cells(r, 4) = "Type B" Then .To = Toemail
If Sheet1.Cells(r, 4) = "Type A" Then .Subject = "Login Information for " + Firstname + " " + Lastname + "
If Sheet1.Cells(r, 4) = "Type B" Then .Subject = "Your other Information is ready - " + Firstname + " " + Lastname
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[NAME]")
oRng.Text = Firstname + " " + Lastname
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[EmpID]")
oRng.Text = EmpID
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[EmailID]")
oRng.Text = EmailID
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[UserID]")
oRng.Text = UserID
Loop
End With
End With
mailItem.Display
r = r + 1
Loop
End Sub
未经测试,因为我没有 Outlook
Option Explicit
Sub Send_email_from_template_my_code_1()
Dim EmpID As String, Lastname As String, Firstname As String
Dim VariableType As String, UserID As String
Dim EmailID As String, Toemail As String, FromEmail As String
Dim mailApp As Object, mailItem As Object
Set mailApp = CreateObject("Outlook.Application")
Dim olInsp As Object, wdDoc As Object, oRng As Object
Dim sText As String, r As Long, template As String
r = 2
Do While Sheet1.Cells(r, 7) <> ""
VariableType = Sheet1.Cells(r, 4)
If VariableType = "Type A" Then
template = "Atemplate.msg"
sText = "Login Information for "
ElseIf VariableType = "Type B" Then
template = "Btemplate.msg"
sText = "Your other Information is ready - "
Else
template = ""
End If
If template <> "" Then
With Sheet1
EmpID = .Cells(r, 1)
Lastname = .Cells(r, 2)
Firstname = .Cells(r, 3)
UserID = .Cells(r, 5)
EmailID = .Cells(r, 6)
Toemail = .Cells(r, 7)
End With
'Call Outlook template
Set mailItem = mailApp.CreateItemFromTemplate("filepath\" & template)
With mailItem
.To = Toemail
.Subject = sText & Firstname & " " & Lastname
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[NAME]")
oRng.Text = Firstname + " " + Lastname
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[EmpID]")
oRng.Text = EmpID
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[EmailID]")
oRng.Text = EmailID
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:="[UserID]")
oRng.Text = UserID
Loop
End With
End With
mailItem.Display
End If
r = r + 1
Loop
Set mailApp = Nothing
End Sub