VBA script IF ELSEIF check if external and internal
excel vba if then multiple statements
excel vba if statement multiple conditions
excel vba if statement based on cell value
else if vba
vba if statement multiple lines
excel vba if without else
excel vba if formula in cell
I'm trying to make the following conditions on a VBA Script for Outlook 2016.
I want users to have a pop up for confirmation when they are sending emails to external users. I also want user to have a pop up confirmation when they are sending email to internal and external users.
Following is the code, but I cant find out how to fix this, because the ElseIf
seems to be ignored.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim recips As Outlook.Recipients Dim recip As Outlook.Recipient Dim pa As Outlook.PropertyAccessor Dim prompt As String Dim Address As String Dim lLen Dim strMyDomain Dim internal As Long Dim external As Long Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" ' non-exchange ' userAddress = Session.CurrentUser.Address ' use for exchange accounts UserAddress = Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress lLen = Len(UserAddress) - InStrRev(UserAddress, "@") strMyDomain = Right(UserAddress, lLen) Set recips = Item.Recipients For Each recip In recips Set pa = recip.PropertyAccessor Address = LCase(pa.GetProperty(PR_SMTP_ADDRESS)) lLen = Len(Address) - InStrRev(Address, "@") str1 = Right(Address, lLen) If str1 = strMyDomain Then internal = 1 If str1 <> strMyDomain Then external = 1 Next If external = 1 Then prompt = "This email is being sent to External addresses. Do you still wish to send?" If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then Cancel = True ElseIf internal + external = 2 Then prompt = "This email is being sent to Internal and External addresses. Do you still wish to send?" If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then Cancel = True End If End If End If End Sub
If the external is true the first 'if' will always be true, which means the code will never get to the 'elseif'.
Rather do
if external + internal = 2 ' Somethen elseif external = 1 ' Somethen else end if
Conditional Statements in Excel VBA, ThenElse Statements (VBA), multiple-line & single-line If-Then statements. ElseIf -> this clause can be used (optionally) if you want to test for multiple conditions. Case statement and VBA Loops (as inner or outer loop), without any limit. Interior.Color = RGB(255, 255, 0). End If. End If. Application.EnableEvents = True VBA - If Elseif - Else statement. An If statement followed by one or more ElseIf statements that consists of boolean expressions and then followed by a default else statement, which executes when all the condition becomes false. Syntax. Following is the syntax of an If Elseif - Else statement in VBScript.
VBA If, ElseIf, Else (Ultimate Guide to If Statements), If Then. VBA If Statements allow you to test if expressions are TRUE or FALSE, running different code based on the results. To determine whether or not a statement is a block If, examine what follows the Then keyword. If anything other than a comment appears after Then on the same line, the statement is treated as a single-line If statement. The Else and ElseIf clauses are both optional. You can have as many ElseIf clauses as you want in a block If, but none can
Following the correct code
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim recips As Outlook.Recipients Dim recip As Outlook.Recipient Dim pa As Outlook.PropertyAccessor Dim prompt As String Dim Address As String Dim lLen Dim strMyDomain Dim internal As Boolean Dim external As Boolean Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" ' non-exchange ' userAddress = Session.CurrentUser.Address ' use for exchange accounts UserAddress = Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress lLen = Len(UserAddress) - InStrRev(UserAddress, "@") strMyDomain = Right(UserAddress, lLen) Set recips = Item.Recipients For Each recip In recips Set pa = recip.PropertyAccessor Address = LCase(pa.GetProperty(PR_SMTP_ADDRESS)) lLen = Len(Address) - InStrRev(Address, "@") str1 = Right(Address, lLen) If str1 = strMyDomain Then internal = True If str1 <> strMyDomain Then external = True Next If external And Not internal Then prompt = "This email is being sent to External addresses. Do you still wish to send?" If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then Cancel = True End If ElseIf internal And external Then prompt = "This email is being sent to Internal and External addresses. Do you still wish to send?" If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then Cancel = True End If End If End Sub
This works great and match all the options i need. Modified the string in bolean. Thanks everyone for the support.
VBA Conditional Statements, Excel VBA Tutorial Part 5: VBA Conditional Statements - If .. Color = 65280 ' Color cell interior green. ElseIf ActiveCell.Value < 10 Then. ActiveCell.Interior. The If Then statement is then exited, without testing any further conditions. See the next section for learning how to use the If, ElseIf..Else statements which is followed by examples including using if statement with Microsoft Excel. Structure of VBA If statements . Following is the general syntax of using If, Elseif and Else VBA statement. Using single line:
Without debating whether True False is better / more intuitive, the code you started with can work with 1 and 2 rather than 1 and 1.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim recips As Outlook.Recipients Dim recip As Outlook.Recipient Dim pa As Outlook.PropertyAccessor Dim prompt As String Dim Address As String Dim lLen Dim strMyDomain Dim internal As Long Dim external As Long Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" ' non-exchange ' userAddress = Session.CurrentUser.Address ' use for exchange accounts UserAddress = Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress lLen = Len(UserAddress) - InStrRev(UserAddress, "@") strMyDomain = Right(UserAddress, lLen) Set recips = Item.Recipients For Each recip In recips Set pa = recip.PropertyAccessor Address = LCase(pa.GetProperty(PR_SMTP_ADDRESS)) lLen = Len(Address) - InStrRev(Address, "@") str1 = Right(Address, lLen) If str1 = strMyDomain Then internal = 1 'If str1 <> strMyDomain Then external = 1 If str1 <> strMyDomain Then external = 2 Next If internal + external = 2 Then prompt = "This email is being sent to External addresses. Do you still wish to send?" If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then Cancel = True ElseIf internal + external = 3 Then prompt = "This email is being sent to Internal and External addresses. Do you still wish to send?" If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then Cancel = True End If End If End If End Sub
JavaScript Conditional Statements: IF, Else, Else IF (Example), Else statement if you have to check two conditions and execute a different set of codes. Try this yourself: This code is editable. Click Run to VBScript ElseIf: Uses. The previous lesson contained an example that let you execute one block of code if the temperature was above a certain temperature and another block of code if it was below a certain temperature. How would we check for another temperature range? For this kind of programming solution you need to use the ElseIf statement.
Internal & External JavaScript: Learn with Example, Net · VBScript · Web Services · WPF If the external.js file is in a different folder, you need to specify the full path to your file in the src attribute. 12); strToAppend = "PM"; } else if (hours <12) { hours1 = "0" + hours; When to Use Internal and External JavaScript Code? If you Software Testing as a Career VBScript If..ElseIf..Else Statements. An If statement followed by one or more ElseIf Statements that consists of boolean expressions and then followed by a default else statement, which executes when all the condition becomes false. Syntax. If(boolean_expression) Then Statement 1 ..
VBA IF Statement - A Complete Guide, It covers Else, ElseIf, conditions and the alternative Select Case statement. 1 Quick Guide to the VBA If Statement; 2 The Webinar; 3 What is the VBA If Statement We're going to use the following test data for the code examples in this post:. While programming, you will have to make certain decisions and perform different actions based on those decisions. In such situations, you will be using conditional statements.
If Then Else Statement in Excel VBA (explained with examples), In Excel VBA, IF Then Else statement allows you to check for a condition, and Multiple IF Then Statements; IF Then Else Statement; IF Then ElseIf Else Statement Dim Cll As Range For Each Cll In Selection If Cll.Value < 0 Then Cll.Interior. ElseIf. Not every condition can be reduced to a simple either/or. Quite often, you'll have more than two options you want to check for. In our code, we may want to check for a value of 10, 11, and any other number. This is where ElseIf comes in. Here's the structure of an ElseIf Statement: If Condition_To_Test Then. ElseIf Condition_To_Test
Comments
- This code is unreadable without proper indentation and with so many IF statements.
- Looks like your
End If
's are in the wrong place. - Sorry Brian i didn't got you,?
- Seem not working as you suggest Hein, any other ideas?
- make sure you use the VBA
=
for equality, not the==
as noted in the answer. Beyond that, what does "not working" mean? You'll need to provide more detail if you want more help. - Hello FreeMan, yes i noticed i should use only = , but actually the second condition is being ignored, i mean now is getting ignored the only external condition. Thanks.
- tested, and not working, getting error on Next Statement
- oops. missed the
End If
after theExit For
. That was thrown together quickly, sorry about that. - I added the End If, but I'm getting same error, could you edit the code please?