我需要在电子邮件到达共享邮箱的收件箱后触发代码。我确实尝试过How do I trigger a macro to run after a new mail is received in Outlook?。
但是,此代码恰好在一个小时后停止触发宏。我在这里想念什么吗?
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")
Dim olRecip As Recipient
Dim Inbox As Outlook.MAPIFolder
' default local Inbox
Set olRecip = objNS.CreateRecipient("abc@xyz.com")
Set Inbox = objNS.GetSharedDefaultFolder(olRecip, olFolderInbox)
Set Items = Inbox.Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
OutlookSplit Msg '//call the function OutlookSplit
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Function OutlookSplit(myItem2 As Outlook.MailItem)
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim objOwner As Object
Dim olInbox1 As Outlook.MAPIFolder
Dim lookInbody As String, lookInbody1() As String
Set olApp = Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set objOwner = olNs.CreateRecipient("abc@xyz.com")
Set olInbox1 = olNs.GetSharedDefaultFolder(objOwner, olFolderInbox).Folders("Current")
lookInbody = myItem2.Body
lookInbody1 = Split(lookInbody, vbCrLf)
End Function