Consulting

Results 1 to 6 of 6

Thread: Outlook download and save file from hyperlink

  1. #1

    Outlook download and save file from hyperlink

    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,

  2. #2
    Where does the hyperlink link to?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Quote Originally Posted by gmayor View Post
    Where does the hyperlink link to?
    The hyperlink link to a web page (https...) and open a new tab to download a file.

  4. #4
    Hmmm. I was afraid of that

    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%2...%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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Quote Originally Posted by gmayor View Post
    Hmmm. I was afraid of that

    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%2...%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

  6. #6
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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