问题描述
||
我在VBA(Excel)中有一个小型项目。我需要打开IE并在我桌面的指定文件夹中下载文件(保存doc)。
但是,我在访问“下载文件”对话框时遇到困难。甚至尝试了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)
If Ret = 0 Then
MsgBox \"Download has been succeed!\"
Else
MsgBox \"Error\"
End If
End Sub
解决方法
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)
If Ret = 0 Then
MsgBox \"Download OK!\"
Else
MsgBox \"Error\"
End If
End Sub