dcsimg
Results 1 to 6 of 6
  1. #1

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    3

    VB script send email internal external statements elseif not working

    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.

    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 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
    Could any body suggest what i'm typing wrong here?

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,554

    Re: VB script send email internal external statements elseif not working

    Thread Moved from ASP/VB Script to Office Development

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung M.2 500 GB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  3. #3
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,554

    Re: VB script send email internal external statements elseif not working

    Looks like you are missing an "End If" after the second "If" and I removed the last End If as it was all just a matter of ordering

    Welcome to the Forums btw!


    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 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
            End If //HERE I ADDED 
        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 Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung M.2 500 GB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  4. #4
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    336

    Re: VB script send email internal external statements elseif not working

    Step through your code.

    Code:
    If external = 1
    This will trigger in every case when external = 1, including when internal also = 1

    Code:
    ElseIf internal + external = 2
    Because of the If statement above, this will trigger when external is NOT equal to 1 AND the sum of internal and external is equal to 2, which, as your code is written, is never.

    There are ways to fix this the way you are doing it, such as changing your initial If statement to something like:

    Code:
    If external = 1 And internal = 0 Then
    but to me the code would read better if your external and internal variables were Boolean

    Code:
    Dim internal As Boolean
    Dim external As Boolean
    Code:
    If str1 = strMyDomain Then internal = True
    If str1 <> strMyDomain Then external = True
    Code:
    If external And Not internal Then
      'Take action for external only recipients
    ElseIf external And internal Then
      'Take action for both internal and external recipients
    End If

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    3

    Re: VB script send email internal external statements elseif not working

    Quote Originally Posted by RobDog888 View Post

    Welcome to the Forums btw!
    Thank you mate.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    3

    Re: VB script send email internal external statements elseif not working

    Quote Originally Posted by OptionBase1 View Post
    Step through your code.

    Code:
    If external = 1
    This will trigger in every case when external = 1, including when internal also = 1

    Code:
    ElseIf internal + external = 2
    Because of the If statement above, this will trigger when external is NOT equal to 1 AND the sum of internal and external is equal to 2, which, as your code is written, is never.

    There are ways to fix this the way you are doing it, such as changing your initial If statement to something like:

    Code:
    If external = 1 And internal = 0 Then
    but to me the code would read better if your external and internal variables were Boolean

    Code:
    Dim internal As Boolean
    Dim external As Boolean
    Code:
    If str1 = strMyDomain Then internal = True
    If str1 <> strMyDomain Then external = True
    Code:
    If external And Not internal Then
      'Take action for external only recipients
    ElseIf external And internal Then
      'Take action for both internal and external recipients
    End If
    Great suggestions, both the proposed solutions worked just fine, and yes i agree with you, bolean variables are far better.
    Thanks for the support

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.