Items_ItemAdd function do not work for Outlook 2013

SLKDS

Registered User.
Local time
Today, 21:04
Joined
Jun 24, 2015
Messages
14
Hi,

I am trying to capture a newly arrived mail in outlook with respective subject line.
The below code works for me on 2010 outlook but when new mail arrive in outlook 2013 ,Mrthod Items_ItemAdd do not get called.

Code:
Option Explicit


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")
' (1) default Inbox
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub


Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim objMsg As MailItem
Dim myAttachments As Outlook.Attachments
Dim FilePath As String
Dim Att As String
Dim MSubject, MBody As Variant
Const attPath As String = "D:\Attachment\" 'Attached file saved ,change if required


Set objMsg = Application.CreateItem(olMailItem)
Set Msg = item

' (2) only act if it's a MailItem
If TypeName(item) = "MailItem" Then

If Msg.Subject Like "*Weekly quality dashboar*" Then 'Change subject as per the required mail

Set myAttachments = Msg.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att
With objMsg
.To = "adf@er.com>
.Subject = Msg.Subject
'now attach it to the new message
.Attachments.Add (attPath & Att)
'.Attachments = Msg.Attachments
.Body = Msg.Body
.Display
End With
objMsg.Send
End If
End If

Set objMsg = Nothing
ProgramExit:
Set objMsg = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
 
Last edited:
One more time:

1. Use code tags -select the code and press # in Advanced
2. If you do not say what is wrong than how do you expect us to guess? Read my signature.
 
This is how you use code tags:

http://www.access-programmers.co.uk/forums/showthread.php?t=240420

With this sort of thing, you will need to put it in a Class, instantiate the Outlook objects in the Initialize sub (or Constructor) and destroy the objects in the Terminate sub (or destructor). And outside of the class you will need to initialise an instance of the class.
 
Someone plz provide solution to my issue
 
The explanation I gave above is how you will do it in Access, however, on closer inspection it would appear that you're trying to do this in Outlook?
 
hi thank you.
The Item object was not getting initialize and so the below code was not running.
 
What I'm asking is, where is the code currently? Inside Outlook or inside an Access db?
 
Right. You've actually posted in the wrong forum, this one is for Access VBA.

I'm on the move at the moment but if you stick around I'll have a look when I'm on a computer and get back to you.
 
You need to change the declaration of the event handler object because it's clashing:
Code:
Private WithEvents [COLOR="blue"]otItems[/COLOR] As Outlook.Items
And then your sub's signature will be:
Code:
Private Sub [COLOR="Blue"]otItems[/COLOR]_ItemAdd(ByVal item As Object)
 

Users who are viewing this thread

Back
Top Bottom