【发布时间】:2014-02-05 23:24:34
【问题描述】:
我正在研究检查电子邮件附件大小的项目,它会在发件人尝试附加大型文档时通知他们。我首先使用http://msdn.microsoft.com/en-us/library/office/aa209975(v=office.11).aspx 中显示的示例代码,它在运行 sub TestAttachAdd() 时效果很好。但是,随着代码的运行,当我手动创建新电子邮件并将文件附加到其中时,不会触发 AttachmentAdd 事件。
我是否错误地使用了私有子“newItem_AttachmentAdd”来执行我正在尝试做的事情?
或者是否有其他 Outlook 事件可用于检测用户何时将文档(使用“附加文件”功能区按钮或通过拖放)附加到新电子邮件?
Public WithEvents newItem As Outlook.MailItem
Private Sub newItem_AttachmentAdd(ByVal newAttachment As Attachment)
If newAttachment.Type = olByValue Then
newItem.Save
If newItem.Size > 500 Then '500 bytes used for testing purposes only
MsgBox "Warning: Item size is now " & newItem.Size & " bytes."
End If
End If
End Sub
Public Sub TestAttachAdd()
Dim olApp As New Outlook.Application
Dim atts As Outlook.Attachments
Dim newAttachment As Outlook.Attachment
Set newItem = olApp.CreateItem(olMailItem)
newItem.Subject = "Test attachment"
Set atts = newItem.Attachments
Set newAttachment = atts.Add("C:\Test.txt", olByValue)
End Sub
-------------- 已更新为 2014 年 1 月 27 日的最新工作版本
Public WithEvents goInspectors As outlook.Inspectors
Public WithEvents newItem As outlook.MailItem
Private Sub Initialize_Handlers()
Set goInspectors = outlook.Application.Inspectors
End Sub
Private Sub Application_Startup()
Initialize_Handlers
End Sub
Private Sub goInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then
Set newItem = Inspector.CurrentItem
End If
End Sub
Private Sub newItem_AttachmentAdd(ByVal newAttachment As Attachment)
If newAttachment.Type = olByValue Then
newItem.Save
If newItem.Size > 500 Then '500 bytes used for testing purposes only
MsgBox "Warning: Item size is now " & newItem.Size & " bytes."
End If
End If
End Sub
【问题讨论】:
标签: vba vb.net outlook outlook-addin