0

我正在编写一个 android 应用程序,我需要一个相当大的数据库。我正在使用 Excel 和 vba 来构建这个数据库。我一直在谷歌搜索,为了下载网页(将数据提取到我的数据库),我想出了下面的代码。但它不起作用。它总是返回 downloadResult=2148270085。有人对解决方案有什么好的建议吗?我在 64 位系统上并使用 EXCEL2013 64 位版本。

Option Explicit
Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
        ByVal pcaller As LongPtr, _
        ByVal szURL As String, _
        ByVal szFileName As String, _
        ByVal dwReserved As LongPtr, _
        ByVal lpfnCB As LongPtr) As LongPtr

Sub DownloadFileButton_Clicked()
    Dim fileURL As String
    Dim fileName As String
    Dim downloadResult As LongPtr
    fileURL = "http://www.wordreference.com/definicion/estar"
    fileName = Application.ThisWorkbook.Path + "\" + "estar.htm"
    downloadResult = URLDownloadToFile(0, fileURL, fileName, 0, 0)
    If downloadResult = 0 then
        Debug.Print "Download started"
    Else
        Debug.Print "Download not started, error code: " & downloadResult
    End If
End Sub
4

1 回答 1

0

好的,我最终使用了 httpRequest 而不是 URLDownloadFile,但也无法使其工作。直到几个小时的测试,我终于发现是我的防火墙阻止了请求。在尝试向我的防火墙添加例外后,我最终在使用我的代码时关闭了防火墙。希望这可以帮助某人。我很确定 URLDownloadFile 也会卡在防火墙中。

Sub DownloadFileButton_Clicked3(language As String, verb As String) ' Fr Es It
    Const WinHttpRequestOption_EnableRedirects = 6
    Dim httpRequest As Object
    Dim URL As String, myString As String
    Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "http://www.wordreference.com/conj/" + language + "Verbs.aspx?v=" + verb
    httpRequest.Option(WinHttpRequestOption_EnableRedirects) = True
    httpRequest.Open "GET", URL, False
    'httpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
    'httpRequest.SetTimeouts  'connection with the server could not be established
    httpRequest.Send
    httpRequest.WaitForResponse
    Debug.Print Len(httpRequest.ResponseText)
    myString = httpRequest.ResponseText
    Dim fileName As String
    fileName = Application.ThisWorkbook.Path + "\" + verb + ".htm"
    Open fileName For Output As #1
    Print #1, myString
    Close #1
    Set httpRequest = Nothing
End Sub
于 2019-03-15T05:27:20.687 回答