JRivest
JRivest

Reputation: 15

Multiple conditions on an Outlook item in Excel VBA

I'm querying Outlook database through Excel VBA, and I want to apply two conditions on the value of a user defined property. The following code, which has both conditions in the same "If" statement

Set cat1 = CurrentItem.UserProperties.Find("catniveau1")
If cat1 <> "" and Not (cat1 Is Nothing) Then 

,raises the following error : "Object variable or With block variable not set". However, having two separate "if" statements is working fine :

Set cat1 = CurrentItem.UserProperties.Find("catniveau1")
If Not (cat1 Is Nothing) Then 
  If cat1 <> "" Then

Is there a way to apply both conditions on the same "if" statement?

Upvotes: 0

Views: 296

Answers (1)

Sgdva
Sgdva

Reputation: 2800

It is not possible to evaluate both conditions at the same time because of logical connections "timing", let me explain.

  1. cat1 is a range (needs to be determined what is going to be first) Since cat1 may or may not be defined you can't evaluate if its <> "".

  2. cat1 <> "" it needs to evaluate what the range is. I know the "human" thinking may be at the same time, but, summarizing:

A)The variable needs to be solved first as range to ...

B)Be evaluated if <> "",

I don't understand why would you need to evaluate if <>"" again -if it didn't find the value it would be nothing- why not only leave the statement If Not (cat1 Is Nothing) Then ?

Upvotes: 1

Related Questions