Outlook: How to extract all URLs from one email
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
- Auto CC/BCC by rules when sending email; Auto Forward Multiple Emails by rules; Auto Reply without exchange server, and more automatic features...
- BCC Warning - show message when you try to reply all if your mail address is in the BCC list; Remind When Missing Attachments, and more remind features...
- Reply (All) With All Attachments in the mail conversation; Reply Many Emails at once; Auto Add Greeting when reply; Auto Add Date&Time into subject...
- Attachment Tools: Auto Detach, Compress All, Rename All, Auto Save All... Quick Report, Count Selected Mails, Remove Duplicate Mails and Contacts...
- More than 100 advanced features will solve most of your problems in Outlook 2021 - 2010 or Office 365. Full features 60-day free trial.
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.
3. Click Tools > References to enable References – Project 1 dialog, tick the Microsoft VBScript Regular Expressions 5.5 checkbox. Click OK.
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.
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.
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.
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.
Note: all of above VBAs extract all types of hyperlinks.
Kutools for Outlook - Brings 100 Advanced Features to Outlook, and Make Work Much Easier!
- Auto CC/BCC by rules when sending email; Auto Forward Multiple Emails by custom; Auto Reply without exchange server, and more automatic features...
- BCC Warning - show message when you try to reply all if your mail address is in the BCC list; Remind When Missing Attachments, and more remind features...
- Reply (All) With All Attachments in the mail conversation; Reply Many Emails in seconds; Auto Add Greeting when reply; Add Date into subject...
- Attachment Tools: Manage All Attachments in All Mails, Auto Detach, Compress All, Rename All, Save All... Quick Report, Count Selected Mails...
- Powerful Junk Emails by custom; Remove Duplicate Mails and Contacts... Enable you to do smarter, faster and better in Outlook.

