Log in

View Full Version : How does OR in VBA work?



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

Aussiebear
10-24-2023, 03:58 AM
Welcome to VBAX Monkeyboard. Outlook VBA code certainly does use the boolean value "or". Below is an example of it in use


'Example 2 with OR
If strSender = "Grandfather" OR strSender="Grandpa" Then
Set obDestFolder = objNameSpace.Folders(mailboxNameString).Folders("Inbox").Folders("Personal")
objVariant.Move obDestFolder
moveOnce = 1
End If


When you suggest that you need to run through the sub three times it suggests that for some reason it's not looping through the "found" emails with either string. I'm sure someone will be along shortly to assist you in this matter. BTW when submitting code to the forum can you please wrap the code with code tags? See the first line in my signature for a hint.

Aussiebear
10-24-2023, 09:58 PM
Since it seems no one else has an idea..... maybe you might like to try this


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) then
olItem.Move myDestFolder
ElseIf (InStr(1, sText, "Es wurden keine Zeilen ausgewählt", vbTextCompare) > 0) Then
olItem.Move myDestFolder
End If
Next
End Sub

monkeyboard
10-25-2023, 01:36 PM
Thank you. But the result is the same.