ItemAdd on Imap Folder get endless loop after saving item

Status
Not open for further replies.

Alex S.

New Member
Outlook version
Outlook 2007
Email Account
IMAP
Hi there

i have a little sub wich check and change the subject.
When it save the subject the ItemAdd Event is firing again and i get an endless loop.
Any ideas what is wrong?

Code:
Private Sub yFld_ItemAdd(ByVal item As Object) 
    If Left(item.Subject, 16) Like "Visit from Mail:" And IsNumeric(Mid(item.Subject, 17)) Then 
        NewSubject = "checked: " & item.Subject & " " & newVisit(CLng(Mid(item.Subject, 17))) 
        If item.Subject <> NewSubject Then 
            item.Subject = NewSubject 
            item.Save 
        End If 
    End If 
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It's an itemadd sub, so it fires when something is added... or saved. All affected messages have checked: as the first word in the subject? Try this instead: If left(lcase(item.Subject), 8) <> "checked:" Then

I'm not sure why it's failing on the item .subject test in the first line the second time around - I'm pretty sure it's failing on the numeric part in the second If.

This should also work in the second if
If Left(item.Subject, 16) Like "Visit from Mail:" then
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
For what it's worth, I tested your macro the best I could (since I don't have the newVisit function) and this works fine, it stops on the item.subject <> newsubject check, as it should. The problem could be with the newVisit function.
Dim NewSubject As String
If Left(item.Subject, 16) Like "Visit from Mail:" And IsNumeric(Mid(item.Subject, 17)) Then
NewSubject = "checked: " & item.Subject & " " & (CLng(Mid(item.Subject, 17)))
If item.Subject <> NewSubject Then
 

Alex S.

New Member
Outlook version
Outlook 2007
Email Account
IMAP
Hello Diane,
i think the check routine could be everything like len(subject)>1 for testing

You say the in your first post the ItemAdd Event is firing when an item is added/saved.
So in my case i can not change and save an item.sibject without a new ItemAdd Event?
 

Alex S.

New Member
Outlook version
Outlook 2007
Email Account
IMAP
Let me ask in other words
Why do i see after the edit and save of the item subject a new Mail Item with the changed subject and the "old" MailItem as deleted?
I would expect that the mail is just updated in the subject.

But in reality it makes a copy of the incomming mail with the changed Subject, saves this copy and deletes the original mail.
Has this something to do with the Imap folder?
 

Alex S.

New Member
Outlook version
Outlook 2007
Email Account
IMAP
Ok, i've got it fixed.
A little bit complicated but "only results count"
I just use a second Folder on my "normal" Inbox to do the changes, so on ItemAdd on the imap folder i only move the mail to my second folder, makes the changes and the move it back to the imap inbox

Code:
Private Sub yFld_ItemAdd(ByVal item As Object) 
    If Left(item.Subject, 16) Like "Visit from Mail:" And IsNumeric(Mid(item.Subject, 17)) Then 
        Debug.Print Now & "  -> " & item.Subject 
        x = "checked: " & item.Subject & " " & newVisit(CLng(Mid(item.Subject, 17))) 
        If item.Subject <> x Then 
            Dim oFld As Outlook.MAPIFolder 
            Set oFld = GetFolder("persönliche ordner\posteingang\visit") 
            item.Move oFld 
        End If 
    End If 
End Sub 
Private Sub vFld_ItemAdd(ByVal item As Object) 
    If Left(item.Subject, 16) Like "Visit from Mail:" And IsNumeric(Mid(item.Subject, 17)) Then 
        x = "checked: " & item.Subject & " " & newVisit(CLng(Mid(item.Subject, 17))) 
        Debug.Print Now & "  -> " & x 
        If item.Subject <> x Then 
            item.Subject = x 
            item.Save 
            Dim oFld As Outlook.MAPIFolder 
            Set oFld = GetFolder("info@xxxxxxu\posteingang") 
            item.Move oFld 
        End If 
    End If 
End Sub
NewVisit is a function which is searching in a database and give back some information

The GetFolder Function i use from Sue Mosher
Code:
Public Function GetFolder(strFolderPath As String) As Outlook.MAPIFolder 
' 
'  Sue Mosher 
' 
Dim objNS As Outlook.NameSpace 
Dim colFolders As Outlook.Folders 
Dim objFolder As Outlook.MAPIFolder 
Dim arrFolders() As String 
Dim i As Long 
On Error Resume Next 
 
arrFolders() = Split(strFolderPath, "\") 
Set objNS = Application.Session 
Set objFolder = objNS.Folders.item(arrFolders(0)) 
If Not objFolder Is Nothing Then 
    For i = 1 To UBound(arrFolders) 
        Set colFolders = objFolder.Folders 
        Set objFolder = Nothing 
        Set objFolder = colFolders.item(arrFolders(i)) 
        If objFolder Is Nothing Then 
            Exit For 
        End If 
    Next 
End If 
 
Set GetFolder = objFolder 
Set colFolders = Nothing 
Set objNS = Nothing 
End Function
i hope this help others who are looking for a solution

Best Alex
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
R Use an ItemAdd to Save Attachments on Arrival Outlook VBA and Custom Forms 0
Diane Poremsky How to use an ItemAdd Macro New Slipstick.com Articles 0
oliv- Treat once an email with the ItemAdd event in a shared mailbox Outlook VBA and Custom Forms 2
B ItemAdd event not functioning - not triggering. Outlook VBA and Custom Forms 2
R outlook 2007 itemadd event handler fails to run Outlook VBA and Custom Forms 2
M Managing Gmail "All Mail" sync issues with IMAP Using Outlook 1
J Outlook 2016 Moving IMAP emails to Exchange Using Outlook 1
K Outlook 2016 - controlling IMAP OST size with Group Policy not working Using Outlook 1
N Sent emails not reflected in all IMAP Folders Using Outlook 4
V Scheduled backup of imap account Using Outlook 7
glnz How set up new IMAP on Outlook-Office 365 and merge in pst from Outlook 2003 for same two email accounts? Using Outlook 5
David Langer Outlook 2016 (365) How to restore the ability to Re-Map iCloud IMAP Folders Using Outlook 5
D Outlook 2013 Yahoo IMAP Sync problems Using Outlook 1
P IMAP Folders Dialog Box Using Outlook 1
O How to recover rules after switch from POP3 to IMAP Using Outlook 2
O Benefits of Exchange over IMAP and why would I choose Exchange? Using Outlook 2
P Desktop doesn't index Outlook IMAP files, laptop Outlook does index those same IMAP files Using Outlook 2
B What is best IMAP .OST file to .PST file converter solutions? Using Outlook 1
I IMAP - are emails dynamically fetched from server as required? Using Outlook 2
I Convert POP3 account (PST) to IMAP (.OST) Using Outlook 3
Mark Foley Color Categories on IMAP mail lost when installing new Windows 7 workstation Using Outlook 12
CWM030 Outlook 2016 with imap emails keep Resurrecting themselves? Using Outlook 5
CWM030 Archiving ON the imap server Using Outlook 3
Y Outlook not displaying all imap emails Using Outlook 5
Y IMAP errors with Outlook 2016 Using Outlook 2
D Outlook 2016 IMAP Connection Returns All Email but outlook.com does NOT Using Outlook.com accounts in Outlook 2
Z Can't delete IMAP folder Using Outlook 2
GaryW88 2016 Archiving IMAP keeping mail on Gmail Server Using Outlook 1
N Error 0x80090326 when trying to setup IMAP account on Outlook.com Using Outlook.com accounts in Outlook 1
M cannot change delivery folders with IMAP accounts Using Outlook 0
E 365 Outlook changed from POP3 to IMAP,all contacts lost Using Outlook 3
O POP3 vs iMAP? Using Outlook 1
B How to 'really' delete IMAP emails? Using Outlook 6
K IMAP Server Wants to alert you to the following: cannpt rename system folder Using Outlook 1
Diane Poremsky Setting up an Outlook.com IMAP account New Slipstick.com Articles 0
M 10 Imap gmail accounts into OL2016-32b want 1 calendar Using Outlook 3
Liz Schneider PST from Outlook 2013 is now IMAP folders in 2016 Using Outlook 10
L Gmail POP, Android IMAP, Outlook 2013 setup close Using Outlook 5
B IMAP folders don't update when Outlook 365 opens Using Outlook 0
J IMAP server Using Outlook 0
J Converted .ost to .pst: Want to Import and Reconnect with IMAP Email Account Using Outlook 2
GregS Import from Outlook.com .ost to IMAP .pst? Using Outlook 3
J Your IMAP server wants to alert you to the following: cannot remove system folder Using Outlook 3
M Outlook 2010 Error: Your IMAP server closed the connection Using Outlook.com accounts in Outlook 1
I Outlook 2016 and Slow access to Outlook.com IMAP Using Outlook 0
E POP vs IMAP on iDevices Using Outlook 3
H Need help setting up GetFolderPath-Makro with Vodafone IMAP Mail-Account Outlook VBA and Custom Forms 0
Mark Foley Where are Outlook categories save for IMAP? Using Outlook 12
P Newly created IMAP subfolders not showing up on email clients away from computer. Using Outlook 3
M Cannot Delete Emails from IMAP inbox Using Outlook 4
Similar threads


















































Top