Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

outllook how to downlod a file from a email link

hi outlook experts, I have a email arrives on my server that I use Outllok to down load the email form my mailserver. I have code that will find and extract an attachment and then save it to my hard disk. But the system uses performs through Joomla to up load an email with a file attached.
But the file does not arrive as an attachment but arrives as a link ie...

First Name D
Last Name nighti
Company newpos
Email address dnighti@abc.com
Attach Text File http://www.abc.com/media/uploads/2007-07-15-19-17-Unknown-samp.TXT 
  ***** this is the file that I wish to down load to my hard drive*****

now if this came as a attachement I could grab it and save it.
But what I need is the object code that will allow me to disect the object module and then down load the file as a data stream or what ever method works and save it as a file.  
I am guessing that I am not the first to run in to this problem, and have search for the last couple of hours with no success. So any one that has a solution or can point me in the right direction I would be most gratefull

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sydneyguy

ASKER

chris
do you have the calling code to this so that I can get it running with out having to figure out the substute varable statements to define that statement
thanks ....
ok have that figured out now have set it up to actually fire when a new file arrives thus the no need for calling object, It has been a while playing with outlook
garry
ok not going yet I found that the system did not run when a  new http link arrived I found an article that memtioned that this should be set
Now, before you put this in the Macro Editor you also probably need to go into Tools, References and go down to "Microsoft XML 6.0" and "Microsoft ActiveX Data Objects 2.8 Library" and enable both those.  I would enable "Microsoft VBScript Regular Expressions 5.5" and actually replace much of the above code with Regex expressions
have sett these but find the system will still not auto run on receipt of a new mail message with a http link attached, thats with it running your code
any ideas ???
Ok bit more info the system mail file does not arrive with an attached http link but it actually comes in as a
mail body subject area and the below comes in a text inside the body area, so the http link will not triger the extract. May need to pull apart the body and extract the info required

Item Value
First Name h
Last Name h
Company h
Email address ga@hotmail.com
Attach Text File http://www.websitewaredesignandhosting.com/media/uploads/2007-07-24-19-42-Unknown-Fjob.txt 
any code to read the body of the mail doc. I have gone down to try and disect the object structure for outlook mail object butit all takes time, could use a point in the right dir
garry

Sorry, wrong end of my day!

If the link is in the subject area then:

    Set mai = Application.Session.GetItemFromID(strEntryId(intFinal))
        If Not mai Is Nothing Then
            If InStr(1, mai.subject, "http:") Then Debug.Print "mail received with a link"
     etc.

It may take time to iron out the scenario but at least in terms of establishing the link string I am sure we can do it as long as we have a format that identifies the specific messages from any chaff you may receive.


Chris
I have now got the link down loaded and can read any part of the file that is in the body which means that I can include any info in the body from the web page and extract the information. So this is good and working fine.
the code that is working although not clean runs from here

***********************************************************************
Private Sub Application_NewMail()  '<-- this must be in the 'ThisOutlookSession'
writeSubject                                 '<-- in the Microsoft Objects Project Folder
End Sub                                       '<--

Public Sub writeSubject()
Dim Str As String
Dim pos1 As Integer
Dim LenYourStr As Integer
Dim YourStr As String

Dim Res As String
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim message As MailItem
Dim filename As String
YourStr = "Attach Work Flow Text File"
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set message = Inbox.Items(1)
MsgBox (Inbox.Items(1))
MsgBox (message.Body)

YourStr = "Attach Work Flow Text File" ''''' Your full string
LenYourStr = Len(YourStr)
Str = message.Body    '''''' Hello the weather is warm here:
pos1 = InStr(message.Body, "Attach Work Flow Text File")
       
Res = Mid(Str, 73 + 1 + 26, Len(Str) - LenYourStr + pos1)
     
     '   End If
Trim (Res)
MsgBox (Res)
Dim sFile
sFile = Res

'To here ****************************************************************
' now as you suggested before I need the code to extract the file and save it on my local drive
' **************************************************************************

' ANY IDEAS ON DOWNLOAD CODE??

'CommonDialog Control

'Commondialog1.ShowSave

'sFile = Mid(sFile, 9) 'get a clean full path & file name

'  With CommonDialog1
 '   .Action = 2
 '   .DialogTitle = "Save File"
 '   .InitDir = ""
 '   .Filter = "Saved Files (*.htm)|*.htm"
 '   .FilterIndex = 1
 '   .Flags = cdlOFNNoValidate + cdlOFNHideReadOnly + lOFNExplorer
 '   .filename = "testname.txt" 'ParsePath(sFile, 6)
 '   .ShowSave
 '   sFile = .filename
 ' End With

 
 
 Dim furl
 Dim filn
 Dim XH
 Dim X
 Dim XG
 
    If Left(Res, 7) = "http://" Then
            furl = Res   '' this should turn out to be "http://www.somesite.com/some/paths/file.zip"
            filn = "C:\Media\" & Mid(furl, InStrRev(furl, "/") + 1) '' this just tries to figure out a filename... so file.zip from above
            Set XH = CreateObject("Microsoft.XMLHTTP") '' use "Microsoft XML 6.0" to download
            XH.Open "GET", furl, 0
            X = XH.Send()
            Set XG = CreateObject("ADODB.Stream") '' use "Microsoft ActiveX Data Objects 2.8 Library" to save to decode and save to disk
            XG.Mode = 3: XG.Type = 1
            X = XG.Open()
            XG.Write (XH.responseBody)
            XG.SaveToFile filn, 2
           ' Exit For    '' only download first link found
    End If
   


End Sub
I have been looking for generic save off the web applications and have so far failed, I have opened xls files but as yet now figured out how to download the file itself ready for saving.  I presume from your bit of code, (If Left(Res, 7) = "http://" Then etc.) that you know a lot more than I so if there is some specific you need an assist with then i'll try and help, otherwise i'll search until I maybe find something I can understand and see if you have a solution for the second leg of your problem ... for now though just glad I was able to help with the first part, and sorry getting the second part working may take a little longer.

Chris
Just found this ...

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
Private Sub Form_Load()
    Dim sSrc As String, sDst As String
    sSrc = "https://www.experts-exchange.com/images/vipAccess.gif"
    sDst = "c:\test.gif"
    Call URLDownloadToFile(0, sSrc, sDst, 0, 0)
End Sub

Chris
Have closed off this question thanks because the next question is a different but is a continuation of the last one. please feel free chris to follow the question all help would be appreciated
garry

https://www.experts-exchange.com/questions/22719771/saving-a-tesxt-file-given-a-http-link-from-a-server-address-to-the-local-drive.html
will test the above last posting
well that did it I just have to integrate it with the other code and it will be a goer, pat your self on the back chris very good, when your in sydney I owe you a beer
garry