访问“下载文件”对话框的难度 - VBA代码

时间:2011-06-15 12:33:40

标签: vba

我在VBA(Excel)中有迷你项目。我需要打开IE并将文件(保存文档)下载到我桌面的指定文件夹中。

但是,我在访问“下载文件”对话框时遇到了困难。甚至尝试过Savekeys概念,但徒劳无功。

如何进一步保存文件?

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

Sub DownloadFileFromWeb()
    Dim myFile As String
    Dim strSavePath As String
    Dim URL As String, ext As String

    URL = "www.ahma.org/Education/BRD_Hardlines_Industry_Item.doc" 'for TEST
    strSavePath = "C:\Users\Yogendra.Ur\Desktop\" & "DownloadedFile" & ext

    Dim buf, Ret As Long
    buf = Split(URL, ".")
    ext = buf(UBound(buf))

    Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.VISIBLE = True
    IE.Navigate URL

    Ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)

    If Ret = 0 Then
        MsgBox "Download has been succeed!"
    Else
        MsgBox "Error"
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

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

Sub DownloadFileFromWeb()

    Dim myFile As String
    Dim buf, Ret As Long
    Dim strSavePath As String
    Dim URL As String, ext As String

    URL = "http://www.ahma.org/Education/BRD_Hardlines_Industry_Item.doc"         
    buf = Split(URL, ".")
    ext = buf(UBound(buf))

    strSavePath = "C:\local files\DownloadedFile." & ext
    Debug.Print strSavePath

    Ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)

    If Ret = 0 Then
        MsgBox "Download OK!"
    Else
        MsgBox "Error"
    End If

End Sub