PDA

View Full Version : Outlook download and save file from hyperlink



NicolasAZ
09-26-2018, 07:36 PM
Hi,

Everyday I receive a couple of email with an hyperlink where I have to click on the hyperlink in order to download the file.
Is there a way with a VBA and a rule to automate the download to a specific folder in my computer. It will apply to some specific email containing "Report Available" in the object and should be sent in C:\Users\TTD2018.

Thanks,

gmayor
09-28-2018, 03:49 AM
Where does the hyperlink link to?

NicolasAZ
09-28-2018, 04:27 AM
Where does the hyperlink link to?

The hyperlink link to a web page (https...) and open a new tab to download a file.

gmayor
09-28-2018, 05:32 AM
Hmmm. I was afraid of that :eek:

However, the following may work for you, either run from a rule or by selecting the message and running the GetMsg macro.
Tested with a message containing a link to a ZIP file on my web site https://www.gmayor.com/Zips/Custom%20Content%20Control.zip
The process does download the file to the folder strPath.
Because you have not said what other links might be in the message or what type of file the link, links to the filter


If InStr(1, oLink.Address, "/") > 0 And _
Left(LCase(oLink.Address), 4) = "http" Then


may be insufficient to identify the file. You may have to make some adjustments to ensure you only get the required link and thus correct file. I have not made any account of the possibility that the filename may already exist. In such a case the existing file will be overwritten.


Option Explicit
Private Declare Function URLDownloadToFile _
Lib "urlmon" Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub GetMsg()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.item(1)
DownloadLinkedFile olMsg
lbl_Exit:
Exit Sub
End Sub

Sub DownloadLinkedFile(olItem As MailItem)
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim oLink As Object
Dim vAddr As Variant
Dim strFName As String
Dim strURL As String
Dim strLocal As String
Const strPath As String = "C:\Path\Attachments\"

On Error Resume Next
With olItem
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
For Each oLink In oRng.hyperlinks
If InStr(1, oLink.Address, "/") > 0 And _
Left(LCase(oLink.Address), 4) = "http" Then
vAddr = Split(oLink.Address, "/")
strFName = vAddr(UBound(vAddr))
strURL = oLink.Address
End If
Next oLink
End With
strLocal = strPath & strFName 'here the drive and download directory
If URLDownloadToFile(0, strURL, strLocal, 0, 0) = 0 Then
MsgBox strFName & " - downloaded"
Else
MsgBox strFName & " - download failed"
End If
lbl_Exit:
Set olInsp = Nothing
Set oRng = Nothing
Set oLink = Nothing
Exit Sub
End Sub

NicolasAZ
09-28-2018, 06:33 AM
Hmmm. I was afraid of that :eek:

However, the following may work for you, either run from a rule or by selecting the message and running the GetMsg macro.
Tested with a message containing a link to a ZIP file on my web site https://www.gmayor.com/Zips/Custom%20Content%20Control.zip
The process does download the file to the folder strPath.
Because you have not said what other links might be in the message or what type of file the link, links to the filter

[/CODE]

The file download is a .xlsx

gmayor
09-28-2018, 08:09 PM
The code should handle that as posted. You could change the filter to


If Right(LCase(oLink.Address), 4) = "xlsx" And _
Left(LCase(oLink.Address), 4) = "http" Then
vAddr = Split(oLink.Address, "/")
strFName = vAddr(UBound(vAddr))
strURL = oLink.Address
End If