0

Program: Outlook 2010
VBA Skill: Novice

Question: Can I use an IF AND statement to change the Category.
I can get it to run if only Item.To or Item.SenderEmailAddress are selected, however I can't get it to run in an AND (I can even get it to run with an OR). I would like to have both as I want to change my category based on the change in email sender address.
store @domain = SupCat admin @domain = SupAdmin etc

It will only pick up the original category associated with the Item.To address, it won't change past there, I have the same code but using the singular Item.To

Example:

'this code does not work, it picks up the example below
If Item.To = "support@domain" And Item.SenderEmailAddress = "store@domain" Then
    Item.Categories = "SupCat"
        strBcc = "bccsup@domain"
            Set objRecip = Item.Recipients.Add(strBcc)
            objRecip.Type = olBCC
                If Not objRecip.Resolve Then
                    strMsg = "Could not resolve the Bcc recipient. " & _
                    "Do you want still to send the message?"
                        res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
                    "Could Not Resolve Bcc Recipient")
                        If res = vbNo Then
                            Cancel = True
                        End If
                End If
End If  

'this code works exactly as written
If Item.To = "support@domain" Then
    Item.Categories = "SupGen"
        strBcc = "bccgen@domain"
            Set objRecip = Item.Recipients.Add(strBcc)
            objRecip.Type = olBCC
                If Not objRecip.Resolve Then
                    strMsg = "Could not resolve the Bcc recipient. " & _
                    "Do you want still to send the message?"
                        res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
                    "Could Not Resolve Bcc Recipient")
                        If res = vbNo Then
                            Cancel = True
                        End If
                End If
End If    

Thank you in advance :)

Update:
I have tried using the GoTo NextItem command in example 1 (before & after the End If) and it still will not pick up the correct change in category or BCC. What is happening is that when the email is made it will only read the 2nd option, the generic category & BCC. It will not recognise the AND command and change the information based on that.

I have just tried it with item.Sender as the option and it will pick up both BCC addresses, but still only give it the 2nd category, not the first.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
MrsAdmin
  • 548
  • 5
  • 12
  • 34
  • In what way does it not work? Does it fail to find any items when the `And` condition is used? Have you tried the 2nd code with `If Item.To = "support@domain" Then` changed to `If Item.SenderEmailAddress = "store@domain" Then` ? – Graham Anderson Feb 21 '14 at 18:29
  • @GrahamAnderson, thanks for commenting. It will only use the code where the singular or the `OR` `IF` is used, it bypassed the `IF AND` code completely. So, it will always default to `SupGen` & the general BCC, it will not recognise the `AND` and change the category or BCC to the first code details. – MrsAdmin Feb 22 '14 at 10:42
  • What you're trying is if the first `If block` is satisfied, it will skip the second `If block`? Then you can add `Exit Sub` if you are done with the routine before `End If` in the first `If block`. If the `If's` are inside a loop, use `Exit For` instead. – L42 Feb 22 '14 at 13:29
  • MrsAdmin I can't see anything wrong with the syntax of the if and statement. Do you know if the `Item.SenderEmailAddress` property has been set? Could you try inserting the line `MsgBox ("_" & Item.SenderEmailAddress & "_")` directly before the if and statement and test it to see what it is, if it's nothing the message box will display __ – Graham Anderson Feb 22 '14 at 15:17
  • @GrahamAnderson, thank you I will try in the morning, it's 0230 here and I'm asleep at the wheel (keyboard), thank you, I will post after I've tried your alternatives. :D Enjoy your night/day :) – MrsAdmin Feb 22 '14 at 15:33

1 Answers1

0

The AND condition is never met. You should find Item.SenderEmailAddress is blank until the mail has been sent.

niton
  • 8,771
  • 21
  • 32
  • 52