Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195

    Return Email Address From Selected Inbox Mail

    Hi Guy's is there any vba code that can grab (return) the senders email address from the current email selected in outlook ?

    ie: the address inside < and > stuart.removedsurname@talktalk.net

    Click image for larger version. 

Name:	Capture.JPG 
Views:	15 
Size:	19.6 KB 
ID:	47249
    Then click on the next mail and it maybe from: <dave@home.com>

    return dave@home.com



    etc.....

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Certainly.
    Code:
    Public Sub DisplaySenderDetails()
    Dim ol As Outlook.Application
    Dim Explorer As Outlook.Explorer
    Dim CurrentItem As Outlook.MailItem
    Dim Sender As Outlook.AddressEntry
    Dim Contact As Outlook.ContactItem
    Set ol = CreateObject("Outlook.Application")
    Set Explorer = ol.ActiveExplorer
    
    
    ' Check whether any item is selected in current folder.
    If Explorer.Selection.Count Then
        ' Get the first selected item.
        Set CurrentItem = Explorer.Selection(1)
        ' Check for type of selected item as only MailItem object has Sender property.
        If CurrentItem.Class = olMail Then
            Set Sender = CurrentItem.Sender
            Debug.Print CurrentItem.SenderEmailAddress
            ' There is no sender if item is in Draft or Outbox
            If Sender Is Nothing Then
                 MsgBox "There is no sender for current email", vbInformation
            Else
                Set Contact = Sender.GetContact
                If Not Contact Is Nothing Then
                    ' Sender is stored in contacts folder so contact item can be displayed.
                    Contact.Display
                Else
                    ' If contact cannot be found, display address entry in properties dialog box.
                    Sender.Details 0
                End If
            End If
        End If
    End If
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    ooohhh Thanks June7, going have a go soon testing your example

    Thank you, will update

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Thanks June7, works great, whilst i will keep the original, the senders details pops up in a contact form, what part of the code can i comment that popup out and just retrieve on a MsgBox ?

    Just i can add data based on current email sender email address

    Kindest

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Quote Originally Posted by DMT Dave View Post
    Thanks June7, works great, whilst i will keep the original, the senders details pops up in a contact form, what part of the code can i comment that popup out and just retrieve on a MsgBox ?

    Just i can add data based on current email sender email address

    Kindest
    Dave, you really need to start understanding any code given you?

    What can you see in the code that will display contact details?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Ahh Contact.Display

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Quote Originally Posted by DMT Dave View Post
    Ahh Contact.Display
    There you go
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    LOL, still struggling to display on a msgbox instead of an outlook contact message, once i can retrieve MsgBox only for testing retrieved, so i can generate a new mail from it

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    I tend to use debug.print as I get sick of responding to msgboxes

    The benefit is you can then copy the output.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What is purpose of MsgBox - to inform user?

    What do you really want to do with this email address?

    If you don't want to check if already in Contacts, remove that If Then block of code and put MsgBox in its place.

    Can use MsgBox instead of Debug.Print.
    Code:
    If Sender Is Nothing Then
         MsgBox "There is no sender for current email", vbInformation
    Else
         MsgBox CurrentItem.SenderEmailAddress
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi June7 thank you, perhaps I should have mentioned also to WGM in project 1 that this would be great with is new email enquiries so no i wouldn't need to check if in contacts, i can grab the email address and respond with an auto response from database

    So I can have the mail body in a text box to view then the user can reply direct from a database option selected.

    I am able to return an email address using InStr if the email address is in the mail body but not direct from outlook which this is so useful

    Thank you June7 and WGM, will adapt this in the morning

    MsgBox was just to check its retrieved, once its retrieved, i would then ditch the MsgBox and send the email address straight to a text box for response purposes.

    Hope this clarifies

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Thank you June7, all works and can now and WGM for your input

    Kindest

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Whether it is a debug.print or a msgbox, again, you need to understand what you have, so you know what you need to retrieve for the data you require.?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi WGM, here we go, for my purpose, this is totally brilliant and works very well indeed, the only question If i may ask, the sender can sometimes have their surname before their forename

    such as Gasman, Welsh and your email address is ie: welshgasman@home.com

    So my txtName is Gasman, Welsh and txtemailAddress is correct, is there a method to reverse but I guess Access doesn't know if your name is the wrong way around

    Would this be Instr function to find the comma ???

    Other than that, this works for my purpose and absolute treat thanks to you and June7

    Code:
    Dim ol As Outlook.ApplicationDim Explorer As Outlook.Explorer
    Dim CurrentItem As Outlook.MailItem
    Dim Sender As Outlook.AddressEntry
    Dim Contact As Outlook.ContactItem
    Set ol = CreateObject("Outlook.Application")
    Set Explorer = ol.ActiveExplorer
    
    
    If Explorer.Selection.Count Then
        Set CurrentItem = Explorer.Selection(1)
        If CurrentItem.Class = olMail Then
            Set Sender = CurrentItem.Sender
            If Sender Is Nothing Then
                MsgBox "There is no sender for current email", vbInformation
            Else
            If CurrentItem.Sender = "form-processor" Then
                MsgBox ("This Is A New Enquiry" & vbNewLine & vbNewLine & _
                "Use The 'Grab Mail Button"), vbInformation
                DoCmd.CancelEvent
                Else
                Me.txtEmailAddress = CurrentItem.SenderEmailAddress
                Me.txtName = CurrentItem.Sender
                Me.txtMailMessage = CurrentItem.Body
                End If
            End If
            End If
    End If

  15. #15
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    I Guess a little button to reverse the name if the surname is left and forename right

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Return active email address
    By DMT Dave in forum Access
    Replies: 10
    Last Post: 08-16-2021, 10:10 AM
  2. Replies: 1
    Last Post: 11-07-2016, 11:18 AM
  3. Email report to value (email address) in a field
    By JackieEVSC in forum Programming
    Replies: 7
    Last Post: 08-28-2015, 11:18 AM
  4. Mail Merge Address Labels
    By Cameron Nicholson in forum Import/Export Data
    Replies: 13
    Last Post: 03-24-2015, 11:07 AM
  5. Replies: 1
    Last Post: 05-01-2014, 11:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums