Skip to main content

Outlook: How to extract all URLs from one email

Author: Sun Last Modified: 2022-04-29

If an email contains hundreds of URLs needed to be extracted to a text file, to copy and paste them one by one will be a tedious job. This tutorial introduces VBAs that can quickly extract all URLs from an email.

VBA to extract URLs from one email to a text file

VBA to extract URLs from multiple emails to an Excel file

Office Tab - Enable Tabbed Editing and Browsing in Microsoft Office, Making Work a Breeze
Unlock Kutools for Outlook's free version now and enjoy over 70 features with unlimited access forever
Boost your Outlook 2021 - 2010 or Outlook 365 with these advanced features. Enjoy 70+ powerful features and elevate your email experience!

VBA to extract URLs from one email to a text file

 

1. Select an email that you want to extract the URLs, and press Alt + F11 keys to enable Microsoft Visual Basic for Applications window.

2. Click Insert > Module to create a new blank module, then copy and paste below code to the module.

VBA: extract all URLs from one email to a text file.

Sub ExportUrlToTextFileFromEmail()
'UpdatebyExtendoffice20220413
  Dim xMail As Outlook.MailItem
  Dim xRegExp As RegExp
  Dim xMatchCollection As MatchCollection
  Dim xMatch As Match
  Dim xUrl As String, xSubject As String, xFileName As String
  Dim xFs As FileSystemObject
  Dim xTextFile As Object
  Dim i As Integer
  Dim InvalidArr
  On Error Resume Next
  If Application.ActiveWindow.Class = olInspector Then
    Set xMail = ActiveInspector.CurrentItem
  ElseIf Application.ActiveWindow.Class = olExplorer Then
    Set xMail = ActiveExplorer.Selection.Item(1)
  End If
  Set xRegExp = New RegExp
  With xRegExp
    .Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*)"
    .Global = True
    .IgnoreCase = True
  End With
  If xRegExp.test(xMail.Body) Then
    InvalidArr = Array("/", "\", "*", ":", Chr(34), "?", "<", ">", "|")
    xSubject = xMail.Subject
    For i = 0 To UBound(InvalidArr)
      xSubject = VBA.Replace(xSubject, InvalidArr(i), "")
    Next i
    xFileName = "C:\Users\Public\Downloads\" & xSubject & ".txt"
    Set xFs = CreateObject("Scripting.FileSystemObject")
    Set xTextFile = xFs.CreateTextFile(xFileName, True)
    xTextFile.WriteLine ("Export URLs:" & vbCrLf)
    Set xMatchCollection = xRegExp.Execute(xMail.Body)
    i = 0
    For Each xMatch In xMatchCollection
      xUrl = xMatch.SubMatches(0)
      i = i + 1
      xTextFile.WriteLine (i & ". " & xUrl & vbCrLf)
    Next
    xTextFile.Close
    Set xTextFile = Nothing
    Set xMatchCollection = Nothing
    Set xFs = Nothing
    Set xFolderItem = CreateObject("Shell.Application").NameSpace(0).ParseName(xFileName)
    xFolderItem.InvokeVerbEx ("open")
    Set xFolderItem = Nothing
  End If
  Set xRegExp = Nothing
End Sub

In this code, it will create a new text file which is named with the email subject and placed in the path: C:\Users\Public\Downloads, you can change it as you need.

doc extract url 1

3. Click Tools > References to enable References – Project 1 dialog, tick the Microsoft VBScript Regular Expressions 5.5 checkbox. Click OK.

doc extract url 1

doc extract url 1

4. Press F5 key or click Run button to run the code, now a text file pops out and all URLs have been extracted in it.

doc extract url 1

doc extract url 1

Note: if you are users of Outlook 2010 and Outlook 365, please also tick the Windows Script Host Object Model checkbox in Step 3. Then click OK.


VBA to extract URLs from multiple emails to an Excel file

 

If you want to extract URLs from multiple selected emails to an Excel file, below VBA code can help you.

1. Select an email that you want to extract the URLs, and press Alt + F11 keys to enable Microsoft Visual Basic for Applications window.

2. Click Insert > Module to create a new blank module, then copy and paste below code to the module.

VBA: extract all URLs from multiple emails to an Excel file

'UpdatebyExtendoffice20220414
Dim xExcel As Excel.Application
Dim xExcelWb As Excel.Workbook
Dim xExcelWs As Excel.Worksheet

Sub ExportAllUrlsToExcelFromMultipleEmails()
  Dim xMail As MailItem
  Dim xSelection As Selection
  Dim xWordDoc As Word.Document
  Dim xHyperlink As Word.Hyperlink
  On Error Resume Next
  Set xSelection = Outlook.Application.ActiveExplorer.Selection
  If (xSelection Is Nothing) Then Exit Sub
  Set xExcel = CreateObject("Excel.Application")
  Set xExcelWb = xExcel.Workbooks.Add
  Set xExcelWs = xExcelWb.Sheets(1)
  xExcelWb.Activate
  With xExcelWs
    .Range("A1") = "Subject"
    .Range("B1") = "DisplayText"
    .Range("C1") = "Link"
  End With
  With xExcelWs.Range("A1", "C1").Font
    .Bold = True
    .Size = 12
  End With
  For Each xMail In xSelection
    Set xWordDoc = xMail.GetInspector.WordEditor
    If xWordDoc.Hyperlinks.Count > 0 Then
      For Each xHyperlink In xWordDoc.Hyperlinks
          Call ExportToExcelFile(xMail, xHyperlink)
      Next
    End If
  Next
  xExcelWs.Columns("A:C").AutoFit
  xExcel.Visible = True
End Sub

Sub ExportToExcelFile(curMail As MailItem, curHyperlink As Word.Hyperlink)
  Dim xRow As Integer
  xRow = xExcelWs.Range("A" & xExcelWs.Rows.Count).End(xlUp).Row + 1
  With xExcelWs
    .Cells(xRow, 1) = curMail.Subject
    .Cells(xRow, 2) = curHyperlink.TextToDisplay
    .Cells(xRow, 3) = curHyperlink.Address
  End With
End Sub

In this code, it extracts all hyperlinks and the corresponding display texts and the email subjects.

doc extract url 1

3. Click Tools > References to enable References – Project 1 dialog, tick Microsoft Excel 16.0 Object Library and Microsoft Word 16.0 Object Library checkboxes. Click OK.

doc extract url 1

doc extract url 1

4. Then place the cursor within the VBA code, press F5 key or click Run button to run the code, now a workbook pops out and all URLs have been extracted in it, then you can save it to a folder.

doc extract url 1

Note: all of above VBAs extract all types of hyperlinks.


Best Office Productivity Tools

Breaking News: Kutools for Outlook Launches Free Version!

Experience the all-new Kutools for Outlook FREE version with 70+ incredible features, yours to use FOREVER! Click to download now!

πŸ€– Kutools AI : Instant pro emails with AI magic--one-click to genius replies, perfect tone, multilingual mastery. Transform emailing effortlessly! ...

πŸ“§ Email Automation: Auto Reply (Available for POP and IMAP)  /  Schedule Send Emails  /  Auto CC/BCC by Rules When Sending Email  /  Auto Forward (Advanced Rules)   /  Auto Add Greeting   /  Automatically Split Multi-Recipient Emails into Individual Messages ...

πŸ“¨ Email Management: Recall Emails  /  Block Scam Emails by Subjects and Others  /  Delete Duplicate Emails  /  Advanced Search  /  Consolidate Folders ...

πŸ“ Attachments ProBatch Save  /  Batch Detach  /  Batch Compress  /  Auto Save   /  Auto Detach  /  Auto Compress ...

🌟 Interface Magic: 😊More Pretty and Cool Emojis   /  Remind you when important emails come  /  Minimize Outlook Instead of Closing ...

πŸ‘ One-click Wonders: Reply All with Incoming Attachments  /   Anti-Phishing Emails  /  πŸ•˜Show Sender's Time Zone ...

πŸ‘©πŸΌβ€πŸ€β€πŸ‘©πŸ» Contacts & Calendar: Batch Add Contacts From Selected Emails  /  Split a Contact Group to Individual Groups  /  Remove Birthday Reminders ...

Instantly unlock Kutools for Outlook with a single clickβ€”permanently free. Don't wait, download now and boost your efficiency!

kutools for outlook features1 kutools for outlook features2
 

 

 

Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations