Consulting

Results 1 to 5 of 5

Thread: Check TO Address in Outlook before sending message

  1. #1
    VBAX Regular
    Joined
    Aug 2021
    Posts
    17
    Location

    Check TO Address in Outlook before sending message

    I'm working on the user-side of this automation project. This workbook of mine emails a form to an employee with some questions on it. I need a little snippet of code to place the values from the form controls into the body of the email before it's sent back to the automation project for further processing.

    I ferreted out the command line to paste the form control data I need, see below...

    Public Sub Application_ItemSend(ByVal item As Object, cancel As Boolean)
        
    item.Body = item.UserProperties("A1").Value & item.Body
    
    End Sub

    This takes the values from field A1 into the body and any additional text from the employee's signature/additions...

    The problem: I can't figure out how to get Outlook to check the TO: Address to determine if it's sending the email back to the automation project. Once I can get that working, I'm certain I can figure out what to do from there.

    TL;DR: I need to check the TO address in an email. I can work out the "what to do after" code. I just need to check if it's a certain address.

    Any advice is deeply appreciated. Thank you!

  2. #2
    From what you have indicated, this isn't going to work. You cannot add code to an e-mail message and the Item_Send code would in any case have to be run from the recipients Outlook.
    As written it would run on ALL e-mail messages so that wouldn't go down well.
    At the very least you would have to install the code on all the user PCs so they could return your forms and then it would need some error handling to restrict it to the required message. Maybe something like the following. Then the need to check the address should be unnecessary and in any case I don't think it possible to change the recipient address at this point.
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim i As Integer
    Dim bProp As Boolean
        With Item
            If TypeName(Item) = "MailItem" Then
                If .UserProperties.Count > 0 Then
                    For i = 1 To .UserProperties.Count
                        If .UserProperties.Item(i).Name = "A1" Then
                            bProp = True
                            Exit For
                        End If
                    Next i
                Else
                    GoTo lbl_Exit
                End If
                If bProp = True Then
                    .Body = .UserProperties("A1").Value & .Body
                End If
            End If
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Aug 2021
    Posts
    17
    Location
    Quote Originally Posted by gmayor View Post
    From what you have indicated, this isn't going to work. You cannot add code to an e-mail message and the Item_Send code would in any case have to be run from the recipients Outlook.
    As written it would run on ALL e-mail messages so that wouldn't go down well.
    At the very least you would have to install the code on all the user PCs so they could return your forms and then it would need some error handling to restrict it to the required message. Maybe something like the following. Then the need to check the address should be unnecessary and in any case I don't think it possible to change the recipient address at this point.
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim i As Integer
    Dim bProp As Boolean
        With Item
            If TypeName(Item) = "MailItem" Then
                If .UserProperties.Count > 0 Then
                    For i = 1 To .UserProperties.Count
                        If .UserProperties.Item(i).Name = "A1" Then
                            bProp = True
                            Exit For
                        End If
                    Next i
                Else
                    GoTo lbl_Exit
                End If
                If bProp = True Then
                    .Body = .UserProperties("A1").Value & .Body
                End If
            End If
        End With
    lbl_Exit:
        Exit Sub
    End Sub

    Thank you for the response, gmayor!

    I'm stumbling my way through what my users will and won't need to install they most definitely will need to install some code to make this automation project work at all. I don't need to worry about modifying recipients, that's handled by some code in the automation project Excel file. The recipient is selected by some logic, then they get an email with a form that prompts them to answer some questions out of radio buttons. The radio buttons' caption value is what I need to send to the .body aspect of the Reply email from the user. This I knew for sure would require code installed, I just couldn't figure out how to specifically identify where the user is sending an email back to the automation project and not to anyone or anything else.

    As you mentioned in your post, the above code snippet I put into my OP would fire for EVERY email, which is not the desired effect.

    Would you help me to understand what your code does exactly? I'm thinking I'm understanding most of it, but I'm not certain. I can see you're cycling through composed emails where there is at least 1 selected recipient and the form field value "A1" is present. Is this behavior intended to be the "catch" for emails back to the automation project? The lbl_Exit statement is to defer emails not to the automation project, right?

  4. #4
    The code operates only on mail items i.e. not other types of object than could be sent.
    As we haven't seen your messages, and you referred to 'item.UserProperties("A1")', the code cycles through item.UserProperties to establish if there is a property 'A1', which would suggest that it is a message to be processed. If there are no user properties the code jumps to lbl_Exit which marks the end of the code. If there is an 'A1' property then its value is added to the start of the message.The code does not look at recipients.
    Without access to your e-mail, this is untested.
    I still think it would make more sense to use a document attachment with content controls that the users could complete and return, then you wouldn't have to worry about providing code and it would be a simple matter to interrogate the forms on their return.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Best Office 365 email signature
    Sigsync is a centralized web-based signature management service that allows you to create and manage your email signatures for your teams and company at one place.It helps to design an attractive and professional company-wide email signature on every email across all the email clients and devices. Sigsync adds profile pictures, company logo, contact details, banners, and social media icons in email signature for a professional look.
    Sigsync is a highly secured ISO 27001:2013 certifiedemail signature service which follows Microsoft guidelines for email flow. It uses Oath 2.0 secured authentication with TLS to create seamless registration and integration and supports Multi Factor Authentication as an additional security feature.
    Sigsync Office 365 email signature provides hundreds of various templatesas well as business-related templates to suit your requirements. It helps you to generate a unique mail signature to promote your business.
    Create professional HTML signatures without any technical background using Sigsync signature generator. It has a WYSIWYG signature generator and automatically handles all the HTML while you place design components such as Section, Text, Social media etc., for your signature.
    Few highlights of Sigsync Office 365 email signature service:

    • Retrieves details from Azure AD and adds in your email signature.
    • Supports creating email signatures and disclaimers for the entire organization.
    • Adds email signatures under all reply and forward emails.
    • Provides the preview of email signature during email compose.
    • Enables signature in sent items.
    • Supports email signatures across all email clients and devices
    • Centrally manages Office 365 email signature for all the employees.
    • User-friendly dashboard to create and customize templates as per the business needs.
    • Automatic configuration of Exchange online connectors.
    • Supports adding company logo, promotional banners, social media links in email signatures
    • Supports web analytics and OneClick surveys in signature

Tags for this Thread

Posting Permissions

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