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.
Similar threads
Thread starter Title Forum Replies Date
Globalforester ItemAdd Macro - multiple emails Outlook VBA and Custom Forms 3
R Use an ItemAdd to Save Attachments on Arrival Outlook VBA and Custom Forms 0
Diane Poremsky How to use an ItemAdd Macro Using Outlook 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
G Outlook 2016: Want IMAP Data Files on My D: Drive and Not C: Drive Using Outlook 1
O Newbie question: how to sync two Outlook -Exchange and IMAP- calendars? Using Outlook 4
e_a_g_l_e_p_i A few question before I decide to switch to Pop from imap Using Outlook 9
P Sending email from outlook IMAP to GMAIL where embedded images are added as attachment Using Outlook 1
L IMAP Emails Disappear Using Outlook 0
V IMAP backup Using Outlook 0
B Emails get stuck in Outbox of Gmail IMAP in Outlook for Windows Using Outlook 0
K Imap PC Outlook + Android phone Using Outlook 1
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
Terry Sullivan Sender's Name Doesn't Appear in the From Field on Outlook 365/IMAP Using Outlook 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 Using Outlook 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

Similar threads

Top