简体   繁体   中英

Close IE after download file by direct link

I use this code to download file from a direct download link via IE. However I stuck when it run to line IE.quit, run time error 462: the remote sever machine does not exist. Here my code:

Sub Download ()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
Dim a, l As Integer
Sheet3.Select
Range("A1").Select
l = Range(Selection, Selection.End(xlDown)).Rows.Count
For a = 1 To l
IE.Visible = True
IE.navigate Cells(a, 2)
Application.Wait (Now + TimeValue("0:00:15"))
Application.SendKeys "%{S}"
Application.Wait (Now + TimeValue("0:00:2"))
IE.Quit
IE = Nothing
Next a
End Sub

Please help me to solve this. Many thanks

I think that's because you create the IE object outside the loop but release the IE object in the loop. You release it in the loop so when it loops for the second time, you can't use it anymore. You can release IE after the loop is finished. Besides, there should be a Set before IE = Nothing .

The code should be like below:

Sub Download()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
Dim a, l As Integer
Sheet2.Select
Range("A1").Select
l = Range(Selection, Selection.End(xlDown)).Rows.Count
For a = 1 To l
IE.Visible = True
IE.navigate Cells(a, 2)
Application.Wait (Now + TimeValue("0:00:15"))
Application.SendKeys "%{S}"
Application.Wait (Now + TimeValue("0:00:2"))
Next a
IE.Quit
Set IE = Nothing
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM