通过起草/发送/发送过程跟踪电子邮件



我想跟踪电子邮件在其生命周期的各个阶段和文件夹中的状态,通过"Drafts"Outbox";,以及";发送";。

相关的兴趣是访问现有电子邮件以收集物业信息的能力,例如发送时间。

我从包含的代码块开始。Do Loop在发送电子邮件时失败,因为变量与电子邮件断开连接。

这导致运行时错误

项目已被移动或删除。

错误编号(错误编号)每次都不一样,我想知道设计目的是什么。

当电子邮件在"草稿"、"发件箱"one_answers"已发送"中移动时,我如何保持与发送电子邮件的联系?

我看到很多关于变量与mailitem断开连接的提及,但没有依赖对象层次结构并避免延迟绑定来解决问题的解决方案。我认为可能会有一个GUID或UUID来标识消息,但正如文档中所示,如果项目被移动,所有属性(如EntryID)都可以更改,不应该依赖这些属性。

通过更深入的检查,这是有意义的,因为电子邮件只是数据库表中的一个记录。如果您在表之间复制/删除记录,信息可能相同或相似,但记录编号可能不会。
此外,这也会影响其他方面:同一封电子邮件可以多次发送,也可以复制/粘贴到不同的文件夹,甚至不同的帐户中。现在,什么是独一无二的?

除了停留";连接的";对于电子邮件,可以使用哪些属性或技术来识别一个?

如果没有";适当的";我唯一能想到的就是使用一个现有的或自定义的字段,比如";标记";属性,以插入UUID。一些公司使用这种技术,在主题行中输入呼叫/订单/支持号码,以便于跟踪。

Dim outlobj As Outlook.Application
Dim mailobj As Outlook.MailItem
Set outlobj = Outlook.Application
Set mailobj = outlobj.CreateItem(olMailItem)
With mailobj
.Recipients.Add "wonderwoman@hallofjustice.com"
.Subject = "Invisible Jet Scheduled Maintenance Reminder"
.Body = "Your invisible jet need to be polished."
.Attachments.Add zipFilename
.Display
.Send
End With
Do
'next line fails due to email moving through Drafts, Outbox, & Sent
'notably, the VBA runtime Err.Num is different each time
'how do i keep the variable connected to a moving target?
If mailobj.Sent = False Then
Sleep 100
Else
MsgBox "The email has been sent."
'other code
Exit Do
End If
Loop

创建一个类并添加MailItem作为该类的事件启用属性。处理诸如打开/写入/发送/保存等事件,以便对电子邮件生命周期进行自定义控制。EntryID是每个邮件项目的唯一属性。


请注意,条目Id仅在项目的第一次保存后生成,并且当用户手动在文件夹之间移动项目时会隐式更改。


以下是一个让您开始的示例:

添加类似于此的类Class1

Option Explicit
Public WithEvents mItem As MailItem
Public id               As String
Private Sub mItem_Open(Cancel As Boolean)
MsgBox "Mail item will be displayed."
id = mItem.EntryID
End Sub

添加具有以下代码的模块:

Option Explicit
Sub test()
Dim cls As New Class1
Dim id  As String
Dim outlobj As Outlook.Application
Dim mailobj As Outlook.MailItem
Set outlobj = Outlook.Application
Set mailobj = outlobj.CreateItem(olMailItem)
Set cls.mItem = mailobj
With mailobj
.Recipients.Add "xx@yy.zz"
.Subject = "Test"
.Body = "Test Content of the e-mail."
.Save
.Display
id = cls.id '/ Store ID for later use.
Debug.Print id
End With

'/ Search that e-mail and display its body contents
Call Retrieve(id)

End Sub

Sub Retrieve(sEntryId As String)
Dim mailobj As Outlook.MailItem
Dim ns As NameSpace
Set ns = GetNamespace("MAPI")
Set mailobj = ns.GetItemFromID(sEntryId)
MsgBox mailobj.Body
End Sub

运行子test

最新更新