+ Reply to Thread
Results 1 to 22 of 22

Extract email addresses from a link (it works but to extend its power)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Extract email addresses from a link (it works but to extend its power)

    Hi everyone,

    I found a nice macro which allows me to extract an email from a weblink as below.

    Function GetEmail(URL As String)
        Dim IE As Object, WebText As String
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Navigate URL
        While IE.ReadyState <> 4
            DoEvents
        Wend
        WebText = IE.Document.body.innerHTML
        IE.Quit
        Set IE = Nothing
        GetEmail = GetEmailAddress(WebText)
    End Function
    
    Function GetEmailAddress(ByVal S As String) As String
        Dim X As Long, AtSign As Long
        Dim Locale As String, Domain As String
        Locale = "[A-Za-z0-9.!#$%&'*/=?^_`{|}~+-]"
        Domain = "[A-Za-z0-9._-]"
        AtSign = InStr(S, "@")
        If AtSign = 0 Then Exit Function
        For X = AtSign To 1 Step -1
            If Not Mid(" " & S, X, 1) Like Locale Then
                S = Mid(S, X)
                If Left(S, 1) = "." Then S = Mid(S, 2)
                Exit For
            End If
        Next
        AtSign = InStr(S, "@")
        For X = AtSign + 1 To Len(S) + 1
            If Not Mid(S & " ", X, 1) Like Domain Then
                S = Left(S, X - 1)
                If Right(S, 1) = "." Then S = Left(S, Len(S) - 1)
                GetEmailAddress = S
                Exit For
            End If
        Next
    End Function
    This macro is quite nice but it is not powerful enought. For example, it just finds anything starting with @. I just wants to search the following keywords for email addresses - if they are found then show it in that cell. If there are more than one keywords found, then the enhanced macro will put additional emails along the line. Please help me improve this code so that: it pulls out all email addresses containing the keywords below.

    admin
    contact
    enquiry
    hello
    info
    mail
    office
    Last edited by BNCOXUK; 02-02-2022 at 04:11 PM.

  2. #2
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Can any VBA guru please look into this query?

  3. #3
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    This could be your function

    Function jec(cell As String) As String
     With CreateObject("vbscript.regexp")
       .Global = True
       .ignorecase = True
       .Pattern = "\b(admin|contact|enquiry|hello|info|mail|office)\b"
        For Each it In Filter(Split(cell), "@", 1)
          If .test(it) Then jec = jec & IIf(jec = "", "", ", ") & it
        Next
     End With
    End Function

  4. #4
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC. Thank you for your help. Your code may not be the one I am looking for.

    For my original code, if I type: =GetEmail("www.londoncarpetandflooring.com"), it will show: [email protected]. Here, 'info' is a keyword. We know other websites may have various emails like office, contact, enquiry. I just need to extract these email addresses. If the email address [email protected] does not contain any of the keywords, then just ignore it.

  5. #5
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    Can you post some sample data with desired results?

  6. #6
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC, please find the attached sample here. A description of the question is added.

    Quote Originally Posted by JEC. View Post
    Can you post some sample data with desired results?
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    Alright, how about this function, instead of the getemailaddress function. This function is separating by comma's when there are more matches (It leaves out double email addresses)
    Note that your file is getting very slow, because it extracts all webpages after every calculation.

    Edit: made small adaption in the code to be more efficiënt.

    Function jec(cell As String) As String
     Dim it, objMatches
     With CreateObject("vbscript.regexp")
       .Global = True
       .ignorecase = True
       .Pattern = "([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"
        Set objMatches = .Execute(cell)
       .Pattern = "\b(" & Join(Application.Transpose(Range("D1:D18")), "|") & ")\b"
        For Each it In objMatches
            If .Test(it) And InStr(1, jec, it, 1) = 0 Then jec = jec & IIf(jec = "", "", ", ") & it
        Next
     End With
    End Function
    Last edited by JEC.; 02-05-2022 at 12:44 PM.

  8. #8
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC, thanks for your kind help again. I may have failed to explain my question clearly.

    I'd better provide an example. Assume a website called: www.xxx.com. On that page, if you check 'View page source', it will show the html script. The page shows many text strings like @&quot, @media, [email protected], ... The original macro I posted will only extract the first text containing @. In this case, the function =GetEmail("www.xxx.com") will simply return @&quot which is not an email at all. That is why I need to test whether the returned text contains a keyword listed above.

    If I directly use your function, then it will not work because your function is based on the condition a proper email has been extracted.

    My question is very simple: how to make a macro which will extract all email addresses in a webpage that contain the keywords in the list? My original macro is simply to extract the first string that contains @. This is not interesting at all.

    The enhanced macro is expected to extract all valid email addresses (with specific keywords) in a webpage and put them in Excel.

  9. #9
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    But if I paste your html page in a cell and use my function, it works as desired I guess.

    See file
    Attached Files Attached Files
    Last edited by JEC.; 02-05-2022 at 12:50 PM.

  10. #10
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC, if I need to paste the html page, then it is not the point of having a macro to automate it. The original macro simply uses a user defined function with the website address as the input to extract data.

  11. #11
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    Pasting the page was just to show you the output of the function. Ofcourse you can use it like you did before. Just switch them.

  12. #12
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC,

    I have to say your macro is incredibly good and neat. I tried - It actually delivered the expected results. Thank you so much. I learned from you as a guru.

    Hope this macro can be valuable to a lot of others in this forum. The credit goes to you!

  13. #13
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC, I tested another site and found the macro resulted in error. Can you please have a look at the attached example here?
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC, if you try it as: =jec(GetEmail("https://www.lazylawn.co.uk/contact")). The result will show #VALUE!

    Thank you for updating it.

  15. #15
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Can anyone please have a look at the query? Thank you.

  16. #16
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    Ok how about

    Function getmail(url As String) As String
     With CreateObject("internetexplorer.application")
       .navigate url
        Do While .ReadyState = 4: DoEvents: Loop
        Do Until .ReadyState = 4: DoEvents: Loop
        getmail = jec(.document.body.innerhtml)
     End With
    End Function
    
    Function jec(cell As String) As String
     Dim it, objMatches
     With CreateObject("vbscript.regexp")
       .Global = True
       .ignorecase = True
       .Pattern = "([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"
        Set objMatches = .Execute(cell)
       .Pattern = "\b(" & Join(Application.Transpose(Range("D1:D20")), "|") & ")\b"
        For Each it In objMatches
            If .Test(it) And InStr(1, jec, it, 1) = 0 Then jec = jec & IIf(jec = "", "", ", ") & it
        Next
     End With
    End Function

    Only call
    =getmail(A2)

  17. #17
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC, your macro is just incredible. It works perfectly. Thank you so much.

    By the way, if I want to extract all emails shown on a webpage, what would be the updated code? There is a nice website for testing:

    HTML Code: 

  18. #18
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    Good to hear! If you want to extract every email, some code can be removed. It still leaves out double emails with this instr-check
    Cheers!

    Function getmail(url As String) As String
     With CreateObject("internetexplorer.application")
       .navigate url
        Do While .ReadyState = 4: DoEvents: Loop
        Do Until .ReadyState = 4: DoEvents: Loop
        getmail = jec(.document.body.innerhtml)
     End With
    End Function
    
    Function jec(cell As String) As String
     Dim it
     With CreateObject("vbscript.regexp")
       .Global = True
       .ignorecase = True
       .Pattern = "([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"
        For Each it In .Execute(cell)
            If InStr(1, jec, it, 1) = 0 Then jec = jec & IIf(jec = "", "", ", ") & it
        Next
     End With
    End Function
    Last edited by JEC.; 02-07-2022 at 03:44 PM.

  19. #19
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Hi JEC, the code really looks powerful and neat. Thanks a lot. It works perfectly well.

    If possible, is it easy to extract a telephone number from a website using a separate function? Not sure if this is possible? Maybe search keywords like Tel:, Phone, Telephone and then extract the string follow these keywords?

    Please forget about it if this sounds difficult to achieve. the email code has been a big help to me.

  20. #20
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    You're welcome! Extracting phone numbers is possible as well, as long as you provide enough sample data Maybe start a different topic about that

  21. #21
    Forum Contributor
    Join Date
    06-18-2012
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    347

    Re: Extract email addresses from a link (it works but to extend its power)

    Many thanks, JEC. I really appreciate your help. The code you made will be useful to a lot of users. I can now end this topic with a nice solution.

  22. #22
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,401

    Re: Extract email addresses from a link (it works but to extend its power)

    nice, goodluck!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to extract First Name from email addresses
    By Howardc1001 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-01-2019, 12:16 PM
  2. extract several email addresses from certain cells
    By bottledwater in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2016, 10:57 AM
  3. [SOLVED] How can I extract email addresses within text
    By sueryan13 in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 10-17-2015, 04:58 AM
  4. [SOLVED] Extract Email Addresses
    By amicman in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-06-2013, 04:33 PM
  5. Extract email addresses from hyperlinks
    By artsy in forum Excel General
    Replies: 1
    Last Post: 11-10-2011, 09:37 AM
  6. Extract Email Addresses From Excel
    By blacksunseven in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-18-2007, 05:48 AM
  7. How to extract email addresses to new column
    By Inquirer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-06-2006, 04:30 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1