monkeyboard
10-24-2023, 02:19 AM
Hello there,
I wrote the following code:
Sub Search_Move_Email()
Dim olItem As Outlook.MailItem
Dim MailItem As Outlook.MailItem
Dim sText As String
Dim myNameSpace As Outlook.NameSpace
Dim myDestFolder As Outlook.MAPIFolder
Dim mySearchFolder As Outlook.MAPIFolder
Set myNameSpace = Application.GetNamespace("MAPI")
Set myDestFolder = myNameSpace.Folders("root").Folders("archiv")
Set mySearchFolder = myNameSpace.Folders("root").Folders("alert-check")
For Each MailItem In mySearchFolder.Items
Set olItem = MailItem
sText = olItem.Body
If (InStr(1, sText, "no rows selected", vbTextCompare) > 0) Or (InStr(1, sText, "Es wurden keine Zeilen ausgewählt", vbTextCompare) > 0) Then
olItem.Move myDestFolder
End If
Next
End Sub
The code is indeed working. But I have to run "serveral times" to move the emails to the archiv folder. The script will firstly move Emails whose body contains "now rows selected". In the 2nd and 3rd run (?it is also strange that I need 3 runs), it will then move Emails whose body contains "Es wurden keine Zeilen ausgewählt".
It seems that the OR doesn't work in VBA?
Thank you for any hints.
Best regards
CC
I wrote the following code:
Sub Search_Move_Email()
Dim olItem As Outlook.MailItem
Dim MailItem As Outlook.MailItem
Dim sText As String
Dim myNameSpace As Outlook.NameSpace
Dim myDestFolder As Outlook.MAPIFolder
Dim mySearchFolder As Outlook.MAPIFolder
Set myNameSpace = Application.GetNamespace("MAPI")
Set myDestFolder = myNameSpace.Folders("root").Folders("archiv")
Set mySearchFolder = myNameSpace.Folders("root").Folders("alert-check")
For Each MailItem In mySearchFolder.Items
Set olItem = MailItem
sText = olItem.Body
If (InStr(1, sText, "no rows selected", vbTextCompare) > 0) Or (InStr(1, sText, "Es wurden keine Zeilen ausgewählt", vbTextCompare) > 0) Then
olItem.Move myDestFolder
End If
Next
End Sub
The code is indeed working. But I have to run "serveral times" to move the emails to the archiv folder. The script will firstly move Emails whose body contains "now rows selected". In the 2nd and 3rd run (?it is also strange that I need 3 runs), it will then move Emails whose body contains "Es wurden keine Zeilen ausgewählt".
It seems that the OR doesn't work in VBA?
Thank you for any hints.
Best regards
CC