Consulting

Results 1 to 4 of 4

Thread: How does OR in VBA work?

  1. #1

    How does OR in VBA work?

    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
    Last edited by Aussiebear; 10-24-2023 at 03:27 AM. Reason: Added code tags to supplied code

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Thank you. But the result is the same.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •