Results 1 to 8 of 8

Thread: Excel macro to download file from a email body URL.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Excel macro to download file from a email body URL.

    I have email which I receive daily consisting of a hyperlink/URL, when i click on that link it gives me the option in internet explorer to open, save or save as, the file link is something like "http://www.somesite.com/servelet/retrievefile?instanceid=777541" now the last six digit keep changing and rest of the URL remains same. What I want to achieve through macro is to look for the recent email and download the file to a specific location. below is what i have so far, i get the error "Run time error '91' object variable or with block variable not set" on sDate = .... line

    I want to download it from a specific outlook folder from a specific subject line email with specific date, all three criteria's should be set through excel worksheet cell.

    Code:
    Option Explicit
    'Declarations
    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 downloadfromurl ()
    Dim itm As Outlook.MailItem    
    Dim bodyString As String    
    Dim bodyStringSplitLine    
    Dim bodyStringSplitWord    
    Dim splitLine    
    Dim splitWord    
    Dim Hyperlink As String    
    Dim LocalFileName As String    
    Dim sURL    As String    
    Dim filename As String    
    'Dim itm As Outlook.MailItem    
    Dim myResult As Integer        
    'Set itm = Application.ActiveInspector.CurrentItem    
    sDate = Format(itm.SentOn, "dd mm yyyy")        
    Const UNC = "myfolder\"
    bodyString = itm.Body    
    bodyStringSplitLine = Split(bodyString, vbCrLf)
        For Each splitLine In bodyStringSplitLine        
    bodyStringSplitWord = Split(splitLine, " ")
            For Each splitWord In bodyStringSplitWord            
    Test = InStr(splitWord, "myhyperlink")            
    If Test = 1 Then            
    Hyperlink = splitWord            
    End If                    
    Next
    Next               
    myResult = URLDownloadToFile(0, Hyperlink, UNC & "sourcefile.xlsx", 0, 0)
        
    If myResult <> 0 Then        
    MsgBox "Error downloading " & Hyperlink & Chr(10) & Error(myResult)    End If     
    itm.UnRead = False
        Set itm = Nothing    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel macro to download file from a email body URL.

    object variable or with block variable not set
    the line above which sets the object is commented out, also the line to dimension the variable
    if you want to search for specific emails first you would need to do that instead, though you would probably still need the variable

    is your code in outlook or?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Excel macro to download file from a email body URL.

    Quote Originally Posted by westconn1 View Post
    *snipp* also the line to dimension the variable
    *snipp*
    It's not. It's his first Line of the Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel macro to download file from a email body URL.

    It's not. It's his first Line of the Sub
    quite right, i missed that one only saw the commented one lower down the list
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Re: Excel macro to download file from a email body URL.

    My code is in excel 2010, how can filter it for a specific subject line and to search in specific outlook folder

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel macro to download file from a email body URL.

    how can filter it for a specific subject line and to search in specific outlook folder
    once you set the correct mapi folder you can use the restrict method of the items collection to filter by subject

    the below code shows how to set an object of the messages collection from the contacts folder, restrict by receivedtime and sort the restricted collection, you can edit to the correct folder and subject line

    Code:
    set olapp = createobject("outlook.application")
    Set msgs = olapp.Session.GetDefaultFolder(olFolderInbox).Items
    Set nm = msgs.Restrict("[ReceivedTime] >= '" & Format(CDate(msg.ReceivedTime), "mm/dd/yyyy hh:nn") & "'")
    nm.Sort ("[receivedtime]")
    you need to check if the outlook constants are valid, if not assign the constant values, or use literal values
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Re: Excel macro to download file from a email body URL.

    Ok. I have added the folder reference below. Now I have the subject line in Cell B2 in the Sheet1 and Received Date in the Cell B3 of Sheet1, how do I refer to the same in the restrict condition.

    Code:
    set olapp = createobject("outlook.application")
    Set msgs = olapp.Session.GetDefaultFolder(olFolderInbox).Folders("ITR").Items
    Set nm = msgs.Restrict("[ReceivedTime] >= '" & Format(CDate(msg.ReceivedTime), "mm/dd/yyyy hh:nn") & "'")
    nm.Sort ("[receivedtime]")

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel macro to download file from a email body URL.

    to restirct by subject try
    Code:
    set nm = msgs.restrict("[Subject}") = '" & range("b2") & "'")
    if you want to restrict by both try like
    Code:
    set nm = msgs.restrict("[Subject]") = '" & range("b2") & "' and "[ReceivedTime] >= '" & Format(range("b3"), "mm/dd/yyyy hh:nn") & "'")
    i did not test either example, so may contain typing or other errors
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width