4

我正在尝试使用 excel VB 宏从受会员密码保护的站点下载 excel 文件。我正在使用“InternetExplorer”对象打开浏览器窗口,登录并浏览到正确的页面,然后在页面中扫描我想要的链接。使用 Workbooks.Open(URLstring) 不起作用,因为没有记录 Excel。它打开的不是实际文件,而是要求登录的 html 页面。

我的偏好是使用 VB 宏在 Internet Explorer 中的正确链接上自动执行右键单击“目标另存为”事件,但我不知道该怎么做。

4

2 回答 2

1

使用 Internet Explorer API 并没有真正的方法来做到这一点。如果它只是一个一次性脚本,您可能可以证明自己使用 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 文档

于 2008-12-13T20:05:53.703 回答
0

If you know the URL of the file you want to download you can use this routine:

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
于 2008-12-15T18:07:40.693 回答