I meant to say "so that there are NOT a lot of nested parens..."
Main Topics
Browse All TopicsI am working with an Office Add-in that works in Outlook and I can't wrap my head around the logic in this statement:
if ((Length(Trim(IMail.To_)) = 0) and
(Length(Trim(IMail.CC)) = 0) and
(Length(Trim(IMail.BCC)) = 0)) or
(Length(Trim(IMail.Subject
begin
ShowMessage('You must fill in To, CC, or BCC and Subject to Save and Tag this message.');
Exit;
end;
I need to show that message if To_, CC, and BCC are all empty and Subject is also empty.
I've tried a few different scenarios but can't get it like I like.
If I fill in the To and Subject, it shouldn't show the message.
If I fill in just the To, it should show.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Your logic is sound, the problem is somewhere else.
try this
var
sTo, sCC, sBCC, sSubject: String;
begin
sTo := Trim(IMail.To_);
sCC := Trim(IMail.CC);
sBCC := Trim(IMail.BCC)
sSubject := Trim(IMail.Subject);
if (sSubject = '') or
((sTo = '') and (sCC = '') and (sBCC = '')) then
begin
ShowMessage('You must fill in To, CC, or BCC and Subject to Save and Tag this message.');
Exit;
end;
place a breakpoint on the if-statement and inspect the variables.
No Jonas, I think the original logic is not correct, and neither is yours. If you read the original request, all three strings AND the subject have to be blank.
So I think my original post is correct. However here is yet another alternate way to check.
Since all the three To CC and BCC must all be blank for this condition, you could simply combine them into one field and test that for length. It would be much easier.
as in
var
Recipients,Subject: string;
begin
Recipients := trim(IMail.To_) + trim(IMail.CC) + trim(Mail.BCC);
Subject := trim(IMail.Subject);
if (Recipients = '') AND (Subject = '') then
begin
ShowMessage('You must fill in To, CC, or BCC and Subject to Save and Tag this message.');
Exit;
end;
to further explain my above code, I combine all three recipient fields (To, CC, BCC) into a single string called "Recipients.".
Thus, if ANY one of them is not blank, this Recipients string will also be not blank.
Then, you can combine that with either an AND or an OR (and if I read your original post correctly, it should be an AND - but I could be wrong....) for the combined condition with the "Subject" and then you should be all set.
"Well, You need to have either a To or BCC, you can't have only a CC, but you can have only a BCC."
OK, then I misread your original post I think. I also think then that NONE of these solutions here will work exactly right, because no one here has the case where just a BCC and a SUBJECT will be OK. Maybe Jonas has it right after all.... :)
There are some contradicting instructions in this thread (I think we all were right at some point, all depending on which lines we read) :)
but if the most recent statement is correct, quoting "Well, You need to have either a To or BCC, you can't have only a CC, but you can have only a BCC." then it doesn't matter if CC is filled in or not, so just leave that out all together from your if-statement.
if ((Length(Trim(IMail.To_)) = 0) and
(Length(Trim(IMail.BCC)) = 0)) or
(Length(Trim(IMail.Subject
begin
ShowMessage('You must fill in To or BCC and Subject to Save and Tag this message.');
Exit;
end;
Hmm. The original request was this:
"I need to show that message if To_, CC, and BCC are all empty and Subject is also empty"
I think I got that request correct, in both my posts. However, as you say Jonas, the instructions seem to be evolving. At this point, I would request a matrix or else this is going to go round and round. Something like
TO CC BCC SUBJ ShowMessage?
Y Y Y Y N
N N N N Y
N N Y ... ....
Business Accounts
Answer for Membership
by: JosephGloszPosted on 2009-06-16 at 15:01:45ID: 24643048
Complex if's are often much easier to debug if you condense out the subparts so that there are a lot of nested parentheses all over. You can always "uncondense" later once it's working OK, if you want.
Plus, it's always easier to work with positive instead of negatives (at least, for me).
for example, let's create a simple function for whether or not you've "got" a field instead of whether you don't have a field.
function Got(S: string): boolean;
begin
result := length(trim(S)) > 0;
end;
with IMail do
if (Got(To_) or Got(CC) or Got(BCC)) and Got(Subject)
then // do nothing
else
begin
ShowMessage;
exit;
end;