Avatar of Rugoingwme
Rugoingwme
 asked on

need a macro that goes to a website and saves weblinks down to my hard drive.

Hi,

I'm not sure of the best or most efficient way to do this, but I want to download files from a website with which  I have a subscription, down to my hard drive.  
I want to do this in a macro, because there are over 800 lessons, and each lesson has about 4-5 pdfs and mp3 files that can be downloaded.

 The website in question is at Chinese Pod.com

http://chinesepod.com/lessons/

I want to download the lessons down to my hard drive.  the lesson downloads are here:
http://chinesepod.com/lessons/ordering-noodles/discussion

I've done downloads from excel to save an existing excel file as another file name, then save that new named file down to my hard drive.  But i'm not sure that I need to do this in excel, especially when the downloads are mp3s and pdfs.  Any suggestions on how to start or what to do would be greatly appreciated.  
VB ScriptDesktopsMicrosoft Excel

Avatar of undefined
Last Comment
Rugoingwme

8/22/2022 - Mon
RobSampson

Hi, you should be able to move most of the code out to a VBS file, with a few adjustments, but that should give you a good starting point.  Can we have a look at the VBA code you've been using?

Regards,

Rob.
ASKER CERTIFIED SOLUTION
sungenwang

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rugoingwme

ASKER
RobSampson,

here's the code that I was using.  It just ended up saving new excel files, but didn't download anything.  

sungenwang.

that code looks impressive!! i'll have to try it when I get home later tonight and see if I can get it to run on my machine.  Thanks in the meantime for the efforts!!

Fred

'        endrow = ws.Range("A1").End(xlDown).Row


'        For i = 2 To endrow
'            slesson = ws.cells(i, 1)


        If slesson = "False" Then Exit Sub
   
        On Error Resume Next


'putting this here so that I can start iterating around the saved spreadsheet.
'need a counter to do this
       
            If slesson = "749" Then
                Exit For
            Else
                Sheets("downloads").Select
                With Selection.QueryTable
                    .Connection = _
                    "URL;http://s3.amazonaws.com/chinesepod/0 & slesson &  /mp3/chinesepod_A & slesson & pb.mp3
                    .WebSelectionType = xlEntirePage
                    .WebFormatting = xlWebFormattingNone
                    .WebPreFormattedTextToColumns = True
                    .WebConsecutiveDelimitersAsOne = True
                    .WebSingleBlockTextImport = False
                    .WebDisableDateRecognition = False
                    .Refresh BackgroundQuery:=False
                End With
                url.SaveAs Filename:= _
                    "D:\Chinese Pod\auto download\080420 " & slesson & " download" _
                    , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
                    ReadOnlyRecommended:=False, CreateBackup:=False
       
'the next below is to complete the number of times this macro is run, then saved to the
lesson number.  it is to go to the next lesson number.
                    n = n + 1
            End If
        Next
RobSampson

sungenwant, well done on producing that code. Excellent work.  Fred, please try that code first, and we'll see how you go with it....there's no point in me re-inventing the wheel!

Regards,

Rob.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sungenwang

Thanks Rob! My vbscript skill has gotten so much better with EE and experts like you!

Rugoingwme

ASKER
sungenwang,

i'm not sure what I did wrong.  I cut/pasted the code you wrote into the macro subroutine that i wrote in VB, and i kept getting compile errors, like "expected end sub". was I supposed to cut/paste your code into something else, or add some other code?

Fred
Rugoingwme

ASKER
moderator,

I was wondering if there is a way to get a email to sungenwang regarding the code he forwarded.  I think it's probably exactly what I need, but I can't get it to run in VB, and I'm not sure what I'm doing wrong.  I wanted to ask a followup question of him, but I suspect that he's moved on to address other questions.  Thanks for any help you can provide.  

My difficulty is that I cut/pasted the code you wrote into the macro subroutine that i wrote in VB, and i kept getting compile errors, like "expected end sub". I have no idea what I did wrong.

Fred
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
sungenwang

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rugoingwme

ASKER
sew,

Thanks. I tried cutting and pasting into notepad, and i'm not presented with an option to save the file as a .vbs extension. I'm only presented with the option of saving it as a text file(.txt) with ANSI, unicode, unicode big endian, or UTF-8. Not sure if this helps, but i'm running Office 2000 on top of Windows XP pro sp2.  

Fred
SOLUTION
sungenwang

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rugoingwme

ASKER
not knowing anything about scripting other than its existence, the last 2 comments by sungenwang were really helpful.  

Fred
Rugoingwme

ASKER
sungenwang,

I tried modifying the script you wrote so that i would download the 860 separate lessons.  I tried modifying the code you wrote to change lines 4 and 27 in your code to 860.  But the new code doesn't seem to work.  Did I miss something?

Fred

strDestPath = "c:\temp"
 
For i = 65 To 70        ' loop through courses A to F
        For j = 1 To 860        ' loop through all possible lessons
                Call FetchFile(strDestPath, "http://s3.amazonaws.com/chinesepod/" & PadStr(j) & "/mp3/chinesepod_" & chr(i) & PadStr(j) & "pb.mp3")
                Call FetchFile(strDestPath, "http://s3.amazonaws.com/chinesepod/" & PadStr(j) & "/mp3/chinesepod_" & chr(i) & PadStr(j) & "pr.mp3")
                Call FetchFile(strDestPath, "http://s3.amazonaws.com/chinesepod/" & PadStr(j) & "/mp3/chinesepod_" & chr(i) & PadStr(j) & "dg.mp3")
                Call FetchFile(strDestPath, "http://s3.amazonaws.com/chinesepod/" & PadStr(j) & "/mp3/chinesepod_" & chr(i) & PadStr(j) & "rv.mp3")
                Call FetchFile(strDestPath, "http://s3.amazonaws.com/chinesepod/" & PadStr(j) & "/pdf/chinesepod_" & chr(i) & PadStr(j) & ".pdf")
        Next
Next
 
msgbox "done!"
 
 
Function FetchFile(strLocation, strFileURL)
        strFileName = Mid(strFileURL, InStrRev(strFileURL, "/")+1)
        strHDLocation = strLocation & "\" & strFileName
 
        ' Fetch the file
        Set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.4.0")
 
        objXMLHTTP.open "GET", strFileURL, False, sign_in_name, sign_in_pswd
        objXMLHTTP.setProxyCredentials proxy_id, proxy_pswd
        objXMLHTTP.send()
 
        If objXMLHTTP.Status = 860 Then
                Set objADOStream = CreateObject("ADODB.Stream")
                objADOStream.Open
                objADOStream.Type = 1 'adTypeBinary
 
                objADOStream.Write objXMLHTTP.ResponseBody
                objADOStream.Position = 0    'Set the stream position to the start
 
                Set objFSO = Createobject("Scripting.FileSystemObject")
                If objFSO.FileExists(strHDLocation) Then objFSO.DeleteFile strHDLocation
                Set objFSO = Nothing
 
                objADOStream.SaveToFile strHDLocation
                objADOStream.Close
                Set objADOStream = Nothing
        Else
                ' do nothing!
        End if
 
        Set objXMLHTTP = Nothing
End Function
 
 
Function PadStr(str)
        Do While Len(str) < 4
                str = "0" & str
        Loop
        PadStr = str
End Function
Your help has saved me hundreds of hours of internet surfing.
fblack61