Items_ItemAdd function do not work for Outlook 2013 (1 Viewer)

SLKDS

Registered User.
Local time
Tomorrow, 00:56
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:

spikepl

Eledittingent Beliped
Local time
Today, 21:26
Joined
Nov 3, 2010
Messages
6,142
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.
 

vbaInet

AWF VIP
Local time
Today, 20:26
Joined
Jan 22, 2010
Messages
26,374
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.
 

SLKDS

Registered User.
Local time
Tomorrow, 00:56
Joined
Jun 24, 2015
Messages
14
Someone plz provide solution to my issue
 

vbaInet

AWF VIP
Local time
Today, 20:26
Joined
Jan 22, 2010
Messages
26,374
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?
 

SLKDS

Registered User.
Local time
Tomorrow, 00:56
Joined
Jun 24, 2015
Messages
14
hi thank you.
The Item object was not getting initialize and so the below code was not running.
 

vbaInet

AWF VIP
Local time
Today, 20:26
Joined
Jan 22, 2010
Messages
26,374
What I'm asking is, where is the code currently? Inside Outlook or inside an Access db?
 

vbaInet

AWF VIP
Local time
Today, 20:26
Joined
Jan 22, 2010
Messages
26,374
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.
 

vbaInet

AWF VIP
Local time
Today, 20:26
Joined
Jan 22, 2010
Messages
26,374
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

Top Bottom