ItemAdd on Imap Folder get endless loop after saving item

Status
Not open for further replies.

Alex S.

New Member
Outlook version
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
 
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
 
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
 
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?
 
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?
 
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
N microsoft 365 IMAP Using Outlook 0
P Yahoo/IMAP folder rename by Outlook desktop 365 Using Outlook 0
O Outlook - Switch from Exchange to IMAP Using Outlook 2
HarvMan Importing PST into IMAP account Using Outlook 12
S Email was migrated from GoDaddy to Microsoft exchange. We lost IMAP ability Exchange Server Administration 1
I Help with Smart Folder + Query Builder on IMAP Using Outlook 0
CWM330 Discussion for all: IMAP + Safety Using Outlook 3
P Copying ALL calendar entries from Calender in PST file to IMAP OST file? Using Outlook 1
TomHuckstep Remove Send/Receive All Folders (IMAP/POP) button from Outlook 365 Ribbon Using Outlook 2
C Trying to move messages between imap accounts/folders Using Outlook 5
R Outlook 2019 accesses POP3 but says its offline (because of IMAP servers?) Using Outlook 0
P "Item could not be moved" message occurs frequently for IMAP inbox, Office 365 Using Outlook 0
B Strange MAPI/Exchange/IMAP behaviour in OL 2016 under10.0.19044.2006 Using Outlook 0
B IMAP server rejects sent email - cannot deliver messages Using Outlook 2
P Comcast IMAP broken but their second level support said to call "the Outlook company" Using Outlook 4
C Outlook 2016 Unable to delete Gmail IMAP calendar Using Outlook 2
K Closing external IMAP email... Outlook 2013 Using Outlook 0
HarvMan Exporting IMAP OST file to PST Using Outlook 5
A Imap account not auto syncing inbox at startup Using Outlook 0
Z Remove GMAIL IMAP account from Outlook 2016 Using Outlook 2
Abraham Outlook 2013 Lost my folders when moving from PST to IMAP Using Outlook 11
I Outlook for Mac 2019 using on desktop and laptop IMAP on both need help with folders Using Outlook 1
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

Similar threads

Back
Top