2011-06-15 54 views
0

我在VBA(Excel)中有迷你項目。我需要打開IE並將文件(保存文檔)下載到桌面上的指定文件夾中。訪問「下載文件」對話框中的Difficulites - VBA代碼

但是,我無法訪問「下載文件」對話框。甚至嘗試過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 
+0

URLDownloadToFile下載獨立的Internet Explorer中,無形。您還必須在url中指定一個協議; HTTP:// WWW .... – 2011-06-15 12:43:24

回答

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