首页 > 解决方案 > 使用 excel vba 在嵌套视图中发送 Outlook 电子邮件

问题描述

我有一个向用户发送两封电子邮件提醒的代码。下面附加的代码运行良好。我的问题是,我希望第二个提醒嵌套在第一个提醒中。

'create session
Dim OutApp As Object
Dim newMail As Object
Dim Emailto, sendfrom As String

'create reply
Dim convo As Conversation
Dim convoItem
Dim entry As String

For J = ws.Cells(5, "C").Value To ws.Cells(6, "C").Value

'get value from combo box
If combovalue = "First Reminder" Then
'MsgBox combovalue

'set a reply
Set OutApp = CreateObject("Outlook.Application")
Set OutNS = OutApp.GetNamespace("MAPI")
entry = ws.Cells(J, "G")
Set mail = OutNS.GetItemFromID(entry) 'get handle on mail item
Set convo = mail.GetConversation 'get handle on existing conversation
Set convoItem = convo.GetRootItems(1) 'get convo root item
Set newMail = convoItem.Reply 'new email as reply to convo
Emailto = ws.Cells(J, "D").Value
sendfrom = "email"

On Error Resume Next
With newMail
.SendUsingAccount = sendfrom
.To = Emailto
.Subject = "Test"
.VotingOptions = "Acknowledge;"
.BodyFormat = olFormatHTML
.HTMLBody = "Body here"
.Send 'or use .Display to open Outlook's new message window before sending
ws.Cells(J, "T").Value = Date
End With

On Error GoTo 0
Set OutApp = Nothing
Set newMail = Nothing
End If

If combovalue = "Second Reminder" Then
'MsgBox ("Correct")
Set OutApp = CreateObject("Outlook.Application")
Set OutNS = OutApp.GetNamespace("MAPI")
entry = ws.Cells(J, "Z")
Set mail = OutNS.GetItemFromID(entry) 'get handle on mail item
Set convo = mail.GetConversation 'get handle on existing conversation
Set convoItem = convo.GetRootItems(1) 'get convo root item
Set newMail = convoItem.Reply 'new email as reply to convo
Emailto = ws.Cells(J, "D").Value
sendfrom = "email"

On Error Resume Next
With newMail
.SendUsingAccount = sendfrom
.To = Emailto
.BCC = ""
.Subject = "Test"
.VotingOptions = "Acknowledge;"
.BodyFormat = olFormatHTML
.HTMLBody = "Body here"
.Send 'or use .Display to open Outlook's new message window before sending
ws.Cells(J, "U").Value = Date
End With

On Error GoTo 0
Set OutApp = Nothing
Set newMail = Nothing
End If
Next J

第一个提醒嵌套在父电子邮件之上,但对于第二个提醒,它不是嵌套在第一个提醒和父电子邮件之上,而是作为嵌套在父电子邮件之上的单独邮件发送。我该如何解决这个问题?

编辑 示例:

1.parent email entry ID AABJ23

2.第一个提醒将通过将entryID设置为AABJ23来回复父电子邮件,然后我将在我发送电子邮件后为第一个提醒获得一个新的条目ID,ABBJ54

3.第二次提醒将通过设置条目ID为ABBJ54回复第一次提醒电子邮件

标签: excelvbaoutlook

解决方案


您正在使用两个不同的 entryID 来检索convo.GetRootItems(1)哪个是原始项目。

entryID 已经标识了您要回复的邮件。

If comboValue = "First Reminder" Then

    entry = ws.Cells(j, "G") ' entryID of the parent mail
    Set Mail = OutNS.GetItemFromID(entry) 'get handle on parent mail
    Set newMail = Mail.reply 'new email as reply to parent mail

End If

If comboValue = "Second Reminder" Then

    entry = ws.Cells(j, "Z") ' entryID of first reminder
    Set Mail = OutNS.GetItemFromID(entry) 'get handle on first reminder item
    Set newMail = Mail.reply 'new email as reply to first reminder

End If

推荐阅读