使用Item_Add将两个结果之一应用于收到的电子邮件 [英] Apply one of two results to incoming email with Item_Add
问题描述
我在ThisOutlookSession中有以下代码.仅第二个子项保存到该文件夹.(当我只有第一部分时,这很完美.)
I have the following code in ThisOutlookSession. Only the second sub is saving to the folder. (When I had only the first part, this worked perfectly.)
是否可以将来自不同域的两个文件保存到各自的文件夹中?
Is there a way both files from different domains can be saved into their own folders?
我正在使用它将文件馈送到Power BI进入的文件夹中并获取最新文件.
I am using this to feed files into folders that Power BI goes into and takes the most recent file.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderName = "it-support@bdmlogistics.com") And _
(Msg.Subject = "Please find attached your MTD Turnover Report") And _
(Msg.Attachments.Count >= 1) Then
'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String
'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "C:\Users\John Smith\OneDrive - Company\Documents\OLAttachments\"
' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att
' mark as read
Msg.UnRead = False
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Private Sub Items_ItemAdd2(ByVal item As Object)
On Error GoTo ErrorHandler
'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderName = "it-support@bdmlogistics.com") And _
(Msg.Subject = "Stock Report by Batch") And _
(Msg.Attachments.Count >= 1) Then
'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String
'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "C:\Users\John Smith\OneDrive - Company\Documents\Stock Reports\"
' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att
' mark as read
Msg.UnRead = False
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
推荐答案
该事件的正式名称为 ItemAdd
.如果将其更改为"ItemAdd2",则它不再是一个事件.
The event is officially named ItemAdd
. If that is changed to "ItemAdd2" it is no longer an event.
而不是 Private Sub Items_ItemAdd2(ByVal项目作为对象)
.您可以将前缀 Items
更改为 Items2
.
Instead of Private Sub Items_ItemAdd2(ByVal item As Object)
. you may change the prefix Items
to Items2
.
通常,这在监视两个文件夹时适用,但可用于同一文件夹.
Normally this would be applicable when two folders are being monitored but can be used for the same folder.
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant
Private WithEvents Items As Items
Private WithEvents Items2 As Items
Private Sub Application_Startup()
Dim myItems As Object
Dim myItems2 As Object
Set myItems = Session.GetDefaultFolder(olFolderInbox).items
Set myItems2 = Session.GetDefaultFolder(olFolderInbox).items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
' ...
End Sub
Private Sub Items2_ItemAdd(ByVal item As Object)
' ...
End Sub
这篇关于使用Item_Add将两个结果之一应用于收到的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!