我正在研究检查电子邮件附件大小的项目,并在他们尝试附加大型文档时通知发件人。我开始使用http://msdn.microsoft.com/en-us/library/office/aa209975(v=office.11).aspx中显示的示例代码,它在运行子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
--------------------------使用当前最新的工作版本1/27/2014进行了更新
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
答案 0 :(得分:0)
您需要将事件处理程序附加到正确的MailItem对象。
捕获Application.Inspectors.NewInspector事件,然后从Inspector.CurrentItem中检索新项目。