使用VB自动化IE“保存目标为”

我试图用一个Excel的VBmacros从会员密码保护的网站下载Excel文件。 我正在使用“InternetExplorer”对象来打开浏览器窗口,login并浏览到正确的页面,然后扫描页面中我想要的链接。 使用Workbooks.Open(URLstring)不起作用,因为Excel不logging。 它将打开要求login的html页面,而不是实际的文件。

我的首选是使用VBmacros自动化在正确的链接上的Internet Explorer中右键单击“保存目标为”事件,但我不知道如何做到这一点。

使用Internet Explorer API没有办法做到这一点。 如果这只是一次性脚本,你可能可以certificate使用SendKeys自己。

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ... Sub YourMacro() ... Navigate IE to the correct document, and get it to pop up the "Save As" dialog ... Set sh = CreateObject("WScript.Shell") sh.AppActivate "File Download" sh.SendKeys "S" Sleep 100 sh.SendKeys "C:\Path\filename.ext{ENTER}" End Sub 

WScript.Shell文档

如果你知道你想下载的文件的URL,你可以使用这个例程:

 Sub HTTPDownloadFile(ByVal URL As String, ByVal LocalFileName As String) Dim http As Object ' Inet Dim Contents() As Byte Set http = New Inet Set http = CreateObject("InetCtls.Inet") With http .protocol = icHTTP .URL = URL Contents() = .OpenURL(.URL, icByteArray) End With Set http = Nothing Open LocalFileName For Binary Access Write As #1 Put #1, , Contents() Close #1 End Sub